Jquery单选按钮提交表单


Jquery Radio Button Submit Form

我在一个页面上有多个表单都是这样的:ID值变化,但选项可以相似。

<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);将导致空数组。

谁能给我指个正确的方向?由于

改变形式,如果这似乎有效?

<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 with working example:) http://jsfiddle.net/QvpH5/

我猜您的页面上有多个窗体,类test。正在发生的事情是,当您使用parents()时,您没有意识到它首先找到所有祖先,然后搜索 .test:first。这将返回第一个表单的值,无论单击哪个表单。

查看jsFiddle查看修复(将。parents更改为。parent): http://jsfiddle.net/SDzzr/