简体   繁体   English

如何通过单击单选按钮将值发送到隐藏的表单域

[英]How to send values to hidden form field by clicking on radio button

In this form i have two radio buttons, if i select any radio button(Book Or Non-Book) that value should send for hidden form field ie if i select book then hidden field value should be 1 and if i select non-book hidden field value should be 2. How can i do this by using jquery or dynamically using jsp either of this 在这种形式我有两个单选按钮,如果我选择任何单选按钮(书或非书),该值应发送隐藏的表单字段,即如果我选择书,那么隐藏的字段值应为1,如果我选择非书隐藏字段值应为2.我如何通过使用jquery或动态使用jsp中的任何一个来完成此操作

1.Before i kept two form in separate div tags with hidden form field on each and i was toggling, like if i select book the form is submitting with the hidden value 1 but if i select non book it was sending null values. 1.在我将两个表单保存在单独的div标签中,每个表格上都有隐藏的表单字段,我正在切换,就像我选择书籍一样,表单提交的是隐藏值1,但如果我选择非书籍则发送空值。

  1. Actually in book form and non-book form all most all fields are same but only hidden value will differ (because in order to know whether user selected book or non-book) but now i thought to keep single form to submit with different hidden values (in above form so many fields are removed to make it simple,sorry if this code have wrong logic) 实际上在书籍形式和非书籍形式中,所有领域都是相同的,但只有隐藏的价值会有所不同(因为为了知道用户选择的书籍还是非书籍)但现在我想保持单一形式提交不同的隐藏价值(在上面的表单中删除了很多字段以使其变得简单,如果此代码具有错误的逻辑,则很抱歉)
    This is my code 这是我的代码

     <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function() { $("[name=toggler]").click(function(){ $('.toHide').hide(); $("#blk-"+$(this).val()).show('slow'); $('input[name=hiddenInput]').val(theValue); }); }); </script> </head> <body> <label><input id="rdb1" type="radio" name="toggler" value="1" />Book</label> <label><input id="rdb2" type="radio" name="toggler" value="2" />Non-Book</label> <div id="blk-1" class="toHide" style="display:none"> <form action="Sample"> <input type="hidden" value="" name="hiddenInput" /> Enter Item Name:<input type="text" name="name"> <input type="submit" name="submit"> </form> </div> 

    Thanks in Advance...! 提前致谢...!

Hope this helps you: 希望这可以帮助你:

http://jsfiddle.net/9BrFs/ http://jsfiddle.net/9BrFs/

$(document).ready(function(){
   $('#rdb1').click(function(){
      $('#hdn').val('1');
   });

   $('#rdb2').click(function(){
      $('#hdn').val('2');
   });

   // ONLY to test the hidden value
   $(':submit').click(function(){
     alert($('#hdn').val());
     return false;
   });
});

give same class name to both radio button, and give id to hidden field 为两个单选按钮指定相同的类名,并为隐藏字段赋予id

 <script type="text/javascript">

   $(document).ready(function() {

     $(".toggle").click(function(){
       $('.toHide').hide();
       var text = $(this).val();
       $('#hdnfield').val(text);
    });
  });
 </script>

Set the id for hidden element and then You can use following code : 设置隐藏元素的ID然后您可以使用以下代码:

$("[name=toggler]").click(function(){
        var theValue = jQuery(this).val();
         // Here 'radio_elem' is the ID of hidden element
         $('#radio_elem').val(theValue);
});
$(':submit').click(function(){
    console.log( $('#radio_elem').val());    

});

Here is the js fiddle : http://jsfiddle.net/XVVmE/ 这是js小提琴: http//jsfiddle.net/XVVmE/

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

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