js获取单选框里面的值

  • Post author:
  • Post category:其他


<form>
	<input type="radio" name="fruit" value="apple" />苹果
	<input type="radio" name="fruit" value="banana" checked />香蕉
	<input type="radio" name="fruit" value="pear" /><input type="checkbox" name="level" value="1" /><span>1</span>
	<input type="checkbox" name="level" value="2" /><span>2</span>
	<input type="checkbox" name="level" value="3" checked /><span>3</span>
	<input type="checkbox" name="level" value="4" /><span>4</span>
	<input type="checkbox" name="level" value="5" checked /><span>5</span>
</form>

<script>
  window.onload = function () {
    var obj = document.getElementsByName('fruit')
    for (var i = 0; i < obj.length; i++) {
      if (obj[i].checked) {
        console.log(obj[i].value)
      }
    }
  }

  var checkboxObj = document.getElementsByName('level')
  check_val = []
  for (k in checkboxObj) {
    if (checkboxObj[k].checked) {
      check_val.push(checkboxObj[k].value)
    }
  }
  console.log(check_val)
</script>



版权声明:本文为weixin_43422861原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。