简体   繁体   English

数组JavaScript中的复选框值

[英]Checkbox value in array javascript

I have a list of checkbox, my goal is to create and store the value of checkbox only the one that is checked. 我有一个复选框列表,我的目标是仅创建并存储复选框的值。 Can someone guide me with this? 有人可以指导我吗?

<input type="checkbox" name="vehicle" value="Bike"/>
<input type="checkbox" name="vehicle" value="Car"/>
<input type="checkbox" name="vehicle" value="Airplane"/>

Please do one thing change name vehicle to vehicle[] 请做一件事将名称vehicle更改为vehicle[]

<input type="checkbox" name="vehicle[]" value="Bike"/>
<input type="checkbox" name="vehicle[]" value="Car"/>
<input type="checkbox" name="vehicle[]" value="Airplane"/>

at post you will receive the array of checked vehicle values and you can store it to DB 在发布后,您将收到检查的车辆值数组,并将其存储到数据库中

in $_POST['vehicle'] $_POST['vehicle']

You really should show some code of what you have tried. 您确实应该显示您尝试过的代码。 You can get the checkboxes by name, then loop over them and collect the values of those that are checked: 您可以按名称获取复选框,然后在其上循环并收集被选中的复选框的值:

<script>

function getCheckboxValues(form) {
  var values = [];
  var vehicles = form.vehicle;

  for (var i=0, iLen=vehicles.length; i<iLen; i++) {
    if (vehicles[i].checked) {
      values.push(vehicles[i].value);
    }
  }
  // Do something with values
  alert("Vehicles: " + values.join(', '));
  return values;
}

</script>

<form onsubmit="getCheckboxValues(this); return false;">
  Bike: <input type="checkbox" name="vehicle" value="Bike"><br>
  Car: <input type="checkbox" name="vehicle" value="Car"><br>
  Aeroplane: <input type="checkbox" name="vehicle" value="Airplane"><br>
  <input type="reset"> <input type="submit">
</form>

Look on live demo 观看现场演示

http://jsbin.com/uwewi http://jsbin.com/uwewi

<script>
$('input[name=vahicle]').click(function() {
    $('input[name=vahicle]:checked').not(this).removeAttr('checked');
});
</script>

OR 要么

jQuery - checkboxes like radiobuttons jQuery-单选按钮之类的复选框

If you want to get only one value among this than please you radio button its best way. 如果您仅想获得其中一个值,请radio按钮为最佳方法。

<input type="radio" name="vehicle" value="Bike"/>
<input type="radio" name="vehicle" value="Car"/>
<input type="radio" name="vehicle" value="Airplane"/>

OR 要么

If you want to go with checkbox than you below jQuery; 如果要使用checkbox比jQuery下面的checkbox多;

<input type="checkbox" name="vehicle" value="Bike"/>
<input type="checkbox" name="vehicle" value="Car"/>
<input type="checkbox" name="vehicle" value="Airplane"/>
<script>
$('input[name=vahicle]').click(function(){
    $('input[name=vahicle]').removeAttr('checked');
    $(this).attr('checked', true);

});
</script>

Hope this will help you...!! 希望这个能对您有所帮助...!!

If you use jQuery: 如果您使用jQuery:

var vehicles = [];
$("input:checked").each(function() {
  vehicles.push($(this).val());
});

console.log(vehicles);

Updated Answer 更新的答案

var input = document.getElementsByTagName('input')
,   value = {0:'', 1:'', 2:''}
,   x = input.length--
,   array
,   box
;
function handle (x) {

 input[x].onchange = function () {
   box      = input[x];
   value[x] = box.checked ? box.value : '';
   array    = [value[0], value[1], value[2]];

   console.log(array);       
 };
}

while (x) handle(--x);

Working fiddle 工作提琴

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

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