简体   繁体   English

如何控制使用jQuery / JavaScript提交哪些表单字段?

[英]How do I control what form fields get submitted with jQuery/JavaScript?

I have a search form that when it gets submitted, it goes to someone elses search site. 我有一个搜索表单,当它提交时,它会转到其他人的搜索站点。 In my form i'm giving users options to choose from. 在我的表格中,我为用户提供了可供选择的选项。 For example, choose to search my site, or external site. 例如,选择搜索我的站点或外部站点。 They get to choose by selecting radio buttons. 他们可以通过选择单选按钮进行选择。

Here's my problem: when searching the external site, because it's also submitting the radio button in the query, the search doesn't work. 这是我的问题:搜索外部网站时,由于它也在查询中提交了单选按钮,因此搜索不起作用。 It's expecting ?SEARCH=Air+Jordan but instead is getting ?inlineRadioOptions=Catalog&SEARCH=Air+Jordan . 期望的是?SEARCH=Air+Jordan ,而是得到?inlineRadioOptions=Catalog&SEARCH=Air+Jordan This throws an error on the external site. 这会在外部站点上引发错误。

Is there a way I can submit this without the radio button? 有没有没有单选按钮可以提交的方法? Here's the code I'm currently using: 这是我当前正在使用的代码:

<form name="catsearchform37675" id="sForm" method="post" action="">
<div style="margin-bottom: 5px;">
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="Catalog" onclick="$('.search_textField').attr({name:'searcharg',id:'searcharg'});$('#searchtype').val('t');$('#sForm').attr({action:'http://www.searchlocal.com/ch~S17/X',target:'_blank',method:'GET'});$('#hiddenField').append('<input type=\'hidden\' name=\'searchtype\' id=\'searchtype\' value=\'t\' />');" /> Catalog
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="Website" onclick="$('.search_textField').attr({name:'CAT_Search',id:'CAT_Search'});$('#sForm').attr({action:'/Default.aspx?SiteSearchID=2960&amp;ID=/search-results',target:'self',method:'POST'});" checked="checked" /> Website
</label>
</div>
<div id="hiddenField"></div>
<div class="clearfix"><input type="text" id="CAT_Search" name="CAT_Search" placeholder="Search" hidefocus="true" style="outline: none medium;" class="search_textField" /> <input type="submit" class="search-btn" value="Search" /></div>
</form>        

如注释中所指出,您必须将单选按钮移出表单。

You can add an event "onsubmit" to form and disable the radio inputs before submit: 您可以在提交前添加事件“ onsubmit”以形成和禁用单选输入:

<form onsubmit="$('input[name=inlineRadioOptions]').prop('disabled',true);return true;" name="catsearchform37675"
  id="sForm" method="post" action="">
<div style="margin-bottom: 5px;">
    <label class="radio-inline">
        <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="Catalog"
               onclick="$('.search_textField').attr({name:'searcharg',id:'searcharg'});$('#searchtype').val('t');$('#sForm').attr({action:'http://www.searchlocal.com/ch~S17/X',target:'_blank',method:'GET'});$('#hiddenField').append('<input type=\'hidden\' name=\'searchtype\' id=\'searchtype\' value=\'t\' />');"/>
        Catalog
    </label>
    <label class="radio-inline">
        <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="Website"
               onclick="$('.search_textField').attr({name:'CAT_Search',id:'CAT_Search'});$('#sForm').attr({action:'/Default.aspx?SiteSearchID=2960&amp;ID=/search-results',target:'self',method:'POST'});"
               checked="checked"/> Website
    </label>
</div>
<div id="hiddenField"></div>
<div class="clearfix"><input type="text" id="CAT_Search" name="CAT_Search" placeholder="Search" hidefocus="true"
                             style="outline: none medium;" class="search_textField"/> <input type="submit"
                                                                                             class="search-btn"
                                                                                             value="Search"/></div>

Here you have a jsfiddle 在这里你有一个jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM