简体   繁体   English

当使用jQuery单击单选按钮时,如何将值推入隐藏的输入类型?

[英]How to push values to input type hidden when clicked on radio button using jQuery?

I am getting values from one variable in array format so by using for loop it will iterate and when click on input type radio button each value with comma separated push to hidden field 我正在从数组格式的一个变量中获取值,因此通过使用for循环,它将进行迭代,并且当单击输入类型单选按钮时,每个值都以逗号分隔并推送到隐藏字段

I tried this but nothing gets inserted. 我尝试了这个,但是没有插入任何东西。 How can I push those values to the hidden field? 如何将这些值推送到隐藏字段?

 var id = ["1", "2"]; // getting this value from another varaible in array format for (var i = 0; i < id.length; i++) { $("input[name=radion_btn" + id[i] + "]").change(function() { $(".selected_val").push(id[i]); //values like 1,2 want to push in hidden field when click on radio button }); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="hidden" name="selected_val[]" value="" class="selected_val" /> <input type="radio" name="radion_btn1" value="" /> <input type="radio" name="radion_btn2" value="" /> 

As per your code. 根据您的代码。 for(var i = 0; i < id.length; i++) { run two times and whenever your event occur. for(var i = 0; i < id.length; i++) {运行两次,只要您的事件发生。 At that time i value come 2 and id[2] comes undefined. 那时我的值变成2,而id [2]变得不确定。 Below code should work. 下面的代码应该工作。

 var id = ["1", "2"]; // getting this value from another varaible in array format arrayData = []; for (var i = 0; i < id.length; i++) { $("input[name=radion_btn" + id[i] + "]").change(function() { console.log($(this).val()); arrayData.push($(this).val()); //values like 1,2 want to push in hidden field when click on radio button $('.selected_val').val(arrayData.join()); }); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="value" name="selected_val[]" value="" class="selected_val" /> <input type="radio" name="radion_btn1" value="1" /> <input type="radio" name="radion_btn2" value="2" /> 

You can simulate a push by adding the hidden input's value before the new value 您可以通过在新值之前添加隐藏输入的值来模拟推送

var id = ["1", "2"]; // getting this value from another varaible in array format

for (var i = 0; i < id.length; i++) {
  $(".selected_val").val("");
  $("input[name=radion_btn" + id[i] + "]").change(function() {
    $(".selected_val").val((i == 0 ? "" : ",") + $(".selected_val").val() + id[i]);
  });
}

Here's an example of one approach that might help. 这是一种可能有用的方法的示例。 See comments in snippet below. 请参见下面的摘要中的评论。

 let obj = {}; // create an empty object to store the clicked values $(".radio").change(function() { // when a radio button is clicked obj[this.id] = $(this).val(); // store it in the object $(".selected_val").val(JSON.stringify(obj)); // and add the object to hidden field as string console.log($(".selected_val").val()); // spit it out to the console }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="hidden" name="selected_val[]" value="" class="selected_val" /> <input type="radio" class="radio" name="radion_btn1" id="1" value="1" />1 <input type="radio" class="radio" name="radion_btn2" id="2" value="2" />2 <input type="radio" class="radio" name="radion_btn3" id="3" value="3" />3 

You'll notice I added a common class for all radio buttons. 您会注意到我为所有单选按钮添加了一个通用类。 This is what I'm attaching the event handler to. 这就是我要附加的事件处理程序。 I also added the IDs to the radio button elements as well. 我也将ID添加到单选按钮元素中。

This may or may not work best for your scenario, but hopefully gets you started in the right direction. 这可能对您的方案最有效,也可能不太有效,但是希望可以使您朝正确的方向入手。

Update 更新

If you'd rather store the values in an array, just change it to an array: 如果您希望将值存储在数组中,只需将其更改为数组即可:

let arr = [];
$(".radio").change(function() {
    arr.push($(this).val());
    $(".selected_val").val(JSON.stringify(arr));
}

Of course, that won't associate the ID with the value like with an object. 当然,这不会将ID和值与对象相关联。

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

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