【问题标题】:Jquery Radio Button Submit FormJquery 单选按钮提交表单
【发布时间】:2023-03-29 02:10:02
【问题描述】:

我在一个页面上有多个表单都是这样的: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.

<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 与工作示例 :) http://jsfiddle.net/QvpH5/

【问题讨论】:

  • 你有没有试过console.dir(CurrectForm)看你是否选对了?

标签: php jquery serialization radio-button submit


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但看起来问题的部分原因在于多个 的表单布局。有什么建议吗?
  • 你能看到我提供的 jsFiddle 中的代码吗?它似乎解决了这个问题
  • 谢谢它确实有效.. 直到您将表格添加到布局中.. 数组为空。有什么办法可以绕过吗?
  • 您可能只是没有足够的遍历 DOM 来满足您的特定用例。只需将更多 .parent() 函数链接在一起,即可获得特定级别。使用 console.dir() 打印出表单值,告诉您何时到达正确的级别
  • 谢谢,我已经部分工作了。但它只从第一个表单中选择值,页面可能有 10 - 60 个表单。这就是我所拥有的 jsfiddle.net/QvpH5
猜你喜欢
相关资源
最近更新 更多
热门标签