简体   繁体   English

jQuery单选按钮提交表单

[英]Jquery Radio Button Submit Form

I have multiple forms on one page all are like this: The ID value changes but the options can be similar. 我在一页上有多种形式,都是这样的: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>

Using JQuery I'm trying to submit this when any of the 3 radio buttons are selected. 当选择三个单选按钮中的任何一个时,我试图使用JQuery提交它。

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

I've used similar to the above with checkboxes and dropdown lists, which have worked fine. 我使用了与上面类似的复选框和下拉列表,效果很好。 But this doesn't seem to submit the values to the do.php page. 但这似乎没有将值提交到do.php页面。 The values that are submitted are show in the div form. 提交的值以div形式显示。 Doing a var_dump($_POST); 做一个var_dump($_POST); results in an empty array. 导致一个空数组。

Can someone point me in the right direction ? 有人可以指出我正确的方向吗? Thanks 谢谢

Changing the form ti this seems to work ? 更改表单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>

Why does changing the cause it to work ? 为什么更改导致它起作用的原因?

Jfiddle with working example :) http://jsfiddle.net/QvpH5/ Jfiddle的工作示例:) http://jsfiddle.net/QvpH5/

My guess is that there are multiple forms on your page with the class test . 我的猜测是您的页面上有类test有多种形式。 What is happening, is when you use parents() , you don't realize it first finds all of the ancestors , and then searches for .test:first . 发生的事情是,当您使用parents() ,您没有意识到它首先找到了所有祖先然后搜索了 .test:first This will return the very first form's values, regardless of which form is clicked. 无论单击哪个表单,这将返回第一个表单的值。

Look at this jsFiddle to see the fix (change .parents to .parent): http://jsfiddle.net/SDzzr/ 查看此jsFiddle以查看修复(将.parents更改为.parent): http : //jsfiddle.net/SDzzr/

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

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