在单个提交中组合两个表单值但单选按钮无法正常工作 - Jquery

巴巴尔

我有 2 个 html 表单,表单 A 和表单 B。我想在提交表单 B 时提交两个表单值。该脚本运行良好。但它实际上传递了所有单选按钮值(男性、女性、其他),只有一个选定的单选按钮值应该传递到下一页。复选框的情况也是如此。

 <form name="A" id="formA">
    mydrink: <input type="text" value="beer" name="mydrink" /><br />

  <input type="radio" name="test" value="male" checked>
  <label for="male">Male</label><br>
  <input type="radio"  name="test"  value="Female">
  <label for="female">Female</label><br>
  <input type="radio"  name="test"  value="other">
  <label for="other">Other</label>

</form>

<br />
<form name="B" id="formB" action="forms.html" method="post">
bar: <input type="text" name="bar" value="doogans" /><br />
<input type="hidden" name="Qnty" value="1">
<input type="submit" value="submit" />
</form>


$(function() {
    $( '#formB input[type = "submit"]' ).on( 'click', function( e ) {
        $.each( $( '#formA' ).prop( 'elements' ), function( idx, elem ) {
            $( '#formB' ).append( $( '<input />' ).attr({
                type : "hidden",
                name : elem.name,
                value : elem.value
            }));
        });
    });
});

斯瓦蒂

您可以检查是否element是收音机以及是否已检查,即:elem.type == "radio" && elem.checked == true在您的每个循环中。

演示代码

$('#formB input[type = "submit"]').on('click', function(e) {
  e.preventDefault()//remove this just for testing
  $.each($('#formA').prop('elements'), function(idx, elem) {
    //check if elem.type is radio and its checked..
    if ((elem.type == "radio" && elem.checked == true) || (elem.type == "text")) {
      $('#formB').append($('<input />').attr({
        type: "text",
        name: elem.name,
        value: elem.value
      }));
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="A" id="formA">
  mydrink: <input type="text" value="beer" name="mydrink" /><br />

  <input type="radio" name="test" value="male" checked>
  <label for="male">Male</label><br>
  <input type="radio" name="test" value="Female">
  <label for="female">Female</label><br>
  <input type="radio" name="test" value="other">
  <label for="other">Other</label>

</form>

<br />
<form name="B" id="formB" action="forms.html" method="post">
  bar: <input type="text" name="bar" value="doogans" /><br />
  <input type="hidden" name="Qnty" value="1">
  <input type="submit" value="submit" />
</form>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jQuery Ajax 表单两个提交按钮在一个表单中

两个或多个按钮单击后,jQuery Timepicker无法正常工作

jQuery clone()表单提交无法正常工作

jQuery:无法使“提交”按钮正常工作

jQuery boostrap modal中的Validaton,无法正常工作。我已经按了两次提交验证按钮

jQuery移动表单数据在单个文件的两个页面之间提交?

jQuery表单提交通过单击链接无法正常工作

两个单选按钮集-JQuery变量

带有两个提交按钮的jQuery Validation插件?

两个具有相同类的Jquery Datatables无法正常工作

jQuery fadeTo()函数在两个元素上均无法正常工作

jQuery datepicker计数两个日期之间的差异将无法正常工作

如何在使用 jQuery 的 HTML 表单中使用两个按钮时将数据提交到烧瓶

使用jQuery控制两个选择输入的组合值

jQuery .submit()无法正常工作,但是提交按钮可以正常工作

由于存在JQuery冲突而导致没有页面刷新的JQuery表单提交无法正常工作

如何使用jQuery基于其他两个单选按钮启用或禁用单选?

使用Jquery和Ajax提交后,表单提交第二次无法正常工作

无法使用 jquery 按钮提交表单

在Jquery中组合两个不同的事件

提交时无法从HTML表单获取jQuery中的值

Jquery 表单验证无法正常工作

jQuery表单验证无法正常工作

表单上的jQuery验证无法正常工作

jQuery表单验证无法正常工作

当两个值相等时,使用jquery显示按钮

将单选值与两个值的乘积相加,以创建一个总jQuery

单个UIViewcontroller中的两个UITableView无法正常工作

jQuery无法正常工作。jQuery的