简体   繁体   English

仅提交表单中的特定字段

[英]submit only specific fields in a form

I have something like this 我有这样的东西

 <div id="list">
 <form>
      <div id="first">
           <label><input type="checkbox" value="third"> third</label>
           <label><input type="checkbox" value="fourth"> fourth</label>
      </div>

      <div id="second">
           <label><input type="checkbox" value="first"> first</label>
           <label><input type="checkbox" value="second"> second</label>
      </div>

     <div id="fake-list-sorted-alphabetically">
           <label><input type="checkbox" value="first"> first</label>
           <label><input type="checkbox" value="second"> second</label>
           <label><input type="checkbox" value="third"> first</label>
           <label><input type="checkbox" value="fourth"> second</label>
     </div>
 </form>
 </div>

would it just be possible with the $.ajax to serialize only the checkbox values from div#first, div#second and ignore everything from #fake? $ .ajax是否有可能仅序列化来自div#first,div#second的复选框值并忽略#fake的所有内容? right now I'm just doing a $('#fake').html(""); 现在我正在做一个$('#fake')。html(“”); right before I submit but that feels hacky. 就在我提交之前,但是感觉很hack。 The reason I don't have #fake outside of the form is because I need it to display in the same container as #first and #second as part of a view all function 我在表单外没有#fake的原因是,作为视图的一部分,我需要它与#first和#second一起显示在同一容器中

Something like: 就像是:

$('#first input').serialize();

Based on your updated question: 根据您更新的问题:

$('form div:not(#fake-list-sorted-alphabetically) :input').serialize()

There're array of input fields: var data = $("#first input").serializeArray(); 输入字段数组: var data = $("#first input").serializeArray();

To submit form with ajax: $.post("you action url", data); 要使用Ajax提交表单, $.post("you action url", data);$.post("you action url", data);

http://api.jquery.com/serializeArray/ http://api.jquery.com/serializeArray/

You can gather just the data you want into an object and pass that object to your ajax call to send it to the server. 您可以只将所需的数据收集到一个对象中,然后将该对象传递给您的ajax调用以将其发送到服务器。 If you're using a submit button, you can bind a click handler to it and use event.preventDefault() on the event to stop it from submitting everything. 如果使用的是提交按钮,则可以将单击处理程序绑定到该按钮,并在事件上使用event.preventDefault()阻止其提交所有内容。 After that, go ahead and use jquery to grab the values you want from the form and then make the ajax call with that data. 之后,继续并使用jquery从表单中获取所需的值,然后使用该数据进行ajax调用。

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

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