2013-03-19 170 views
2

我在一个页面上有多个表单都是这样的:ID值改变但选项可以类似。Jquery单选按钮提交表单

<form class="test"><td>4000</td> 
<td align='center'><input type='radio' name='radio' value='car' checked></td> 
<td align='center'><input type='radio' name='radio' value='boat' ></td> 
<td align='center'><input type='radio' name='radio' value='Plane' ></td> 
<input type='hidden' name='id' value='4000'/> 
<input type='hidden' name='location' value='local'/> 
<div class="form" style="display: none;"> 
</div></form> 

使用JQuery我试图提交这个时,任何3个单选按钮被选中。

<script> 
$("input[name='radio']").click(function() { 
    var CurrectForm=$(this).parents('.test:first'); 
    $.post(
     'do.php', 
     CurrectForm.serialize(), 
     function(response) { 
      CurrectForm.find('#form').html(response); 
       show(response); 
     } 
    ); 
}); 
</script> 

我用类似上面复选框和下拉列表,已经工作得很好。但是这似乎并没有将这些值提交给do.php页面。提交的值以div格式显示。做一个var_dump($_POST);会产生一个空的数组。

有人能指出我正确的方向吗? 谢谢

改变形式ti这似乎工作?

<td><form class="test"> 
<input type='radio' name='radio' value='car' checked> 
<input type='radio' name='radio' value='boat' > 
<input type='radio' name='radio' value='Plane' > 
<input type='hidden' name='id' value='4000'/> 
<input type='hidden' name='location' value='local'/> 
<div class="form" style="display: none;"> 
</div></form></td> 

为什么改变原因它工作?

Jfiddle与工作示例:) http://jsfiddle.net/QvpH5/

+0

你有没有试过'console.dir(CurrectForm)'来看看你是正确的选择呢? – JoeCortopassi 2013-03-19 18:33:50

回答

0

我的猜测是,有你的页面上有多个形式与类test。发生什么事情是,当你使用parents(),你不知道它首先发现所有的祖先,,然后搜索.test:first。无论点击哪个表单,这将返回第一个表单的值。

看这个的jsfiddle看到修复(其他城市。家长到.parent):http://jsfiddle.net/SDzzr/

+1

谢谢,但看起来问题部分是由于多个的表单布局造成的。 有什么建议吗? – MacMan 2013-03-19 19:58:04

+0

你能看到我提供的jsFiddle中的代码吗?它似乎解决了问题 – JoeCortopassi 2013-03-20 14:36:09

+0

感谢它的工作..直到你添加一个表到布局..数组是空的。任何方式通过传递? – MacMan 2013-03-20 18:20:55