2013-07-02 34 views
0

我有这样的事情只提交特定字段的形式

<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> 

将它只是可以用$就以连载只能从DIV#第一,DIV#第二复选框值,而忽略了从#假?现在我只是在做一个$('#fake')。html(“”);就在我提交之前,但感觉很不好。我没有形式的#fake以外的原因是因为我需要它作为#first和#second相同的容器作为视图的一部分,以显示所有功能

+0

好像它会更容易/清洁只是有不同的形式。 – crowder

+0

只需放置'form'元素的'#fake' _outside_ ... – CBroe

+0

为什么您在输入中省略了名称属性? –

回答

2

喜欢的东西:

$('#first input').serialize(); 
根据您的更新问题

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

匿名选民:关心讨论? –

+0

修改原始问题,如果有多个div我想序列化? – archytect

+0

那么你真的要求所有的表单字段,除了'#伪名单 - 按字母顺序排序吗?你可以有100个div你想要的价值和一个你不需要? –

1

您可以收集你想要的数据转换成一个对象,该对象传递给你的Ajax调用它发送到服务器。如果您使用提交按钮,您可以将一个点击处理程序绑定到它并在事件上使用event.preventDefault()来阻止它提交所有内容。之后,继续并使用jquery从表单中获取所需的值,然后使用该数据进行ajax调用。