简体   繁体   English

如果使用 javascript 选中至少一个复选框,则启用按钮

[英]Enable a button if atleast one checkbox is checked using javascript

How to enable the submit button when I check atleast 1 checkbox?当我选中至少 1 个复选框时如何启用提交按钮?

 $("input[name='vehicle']"), submitButt = $("input[name='Submit']"); var checkboxes = $("input[name='vehicle']"), submitButtList = $("#Submit"); checkboxes.click(function() { submitButt.attr("disabled", !checkboxes.is(":checked")); if (!checkboxes.is(":checked")) { submitButtList.addClass("disabled"); } else { submitButtList.removeClass("disabled"); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car"> I have a car<br> <input type="submit" value="Submit" disabled>

Here is a JavaScript solution, since you have tagged Javascript and not Jquery这是一个JavaScript解决方案,因为您标记了Javascript而不是Jquery

 function callFunction() { var checkboxes = document.querySelectorAll('input[type="checkbox"]'); var checkedOne = Array.prototype.slice.call(checkboxes).some(x => x.checked); document.querySelectorAll('input[type="submit"]')[0].disabled = true; if (checkedOne) { document.querySelectorAll('input[type="submit"]')[0].disabled = false; } }
 <input type="checkbox" name="vehicle" onchange="callFunction()" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" onchange="callFunction()" value="Car"> I have a car<br> <input type="submit" value="Submit" disabled>

Jquery solution: jQuery 解决方案:

Check the length of checked checkboxes and use prop to disable or enable the button.检查选中复选框的长度并使用prop禁用或启用按钮。

 $("input[name='vehicle']").on('change', function() { $("input[type='submit']").prop('disabled', !$("input[name='vehicle']:checked").length); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car"> I have a car<br> <input type="submit" value="Submit" disabled>

give the same id for the check boxes and write javascript codes on the on click event on the check boxes which enables/disables the button if any one is clicked.为复选框提供相同的 id,并在复选框上的 on click 事件上编写 javascript 代码,如果单击任何一个,则启用/禁用按钮。

by giving the same name enables only one of the two to be selected通过提供相同的名称,只能选择两者之一

Try this might help you试试这可能对你有帮助

 function myFunction() { if (document.getElementById('bike').checked || document.getElementById('car').checked) { document.getElementById("btn").disabled = false; } else { document.getElementById("btn").disabled = true; } }
 <input id="bike" type="checkbox" onchange="myFunction()" name="vehicle" value="Bike"> I have a bike<br> <input id="car" type="checkbox" onchange="myFunction()" name="vehicle" value="Car"> I have a car<br> <input type="submit" id="btn" value="Submit" disabled>

add name to submit button.添加名称以提交按钮。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"> I have a car<br>
<input type="submit" value="Submit" name="Submit" disabled>

1. Suggestion to improve your code 1. 改进代码的建议

You have to add the "name" attribute to your in order to run your code as expected:您必须将“名称”属性添加到您的,以便按预期运行您的代码:

 $("input[name='vehicle']"), submitButt = $("input[name='Submit']"); var checkboxes = $("input[name='vehicle']"), submitButtList = $("#Submit"); checkboxes.click(function() { submitButt.attr("disabled", !checkboxes.is(":checked")); if (!checkboxes.is(":checked")) { submitButtList.addClass("disabled"); } else { submitButtList.removeClass("disabled"); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car"> I have a car<br> <input type="submit" name="Submit" value="Submit" disabled>

2. Suggestion to improve Milan Chheda plain javascript code 2. 改进Milan Chheda纯javascript代码的建议

If you want, you can also use enter link description here First answer with plain javascript in a shorter form:如果需要,您也可以在此处使用输入链接描述以较短形式使用纯 javascript 的第一个答案

You can replace:您可以替换:

  document.querySelectorAll('input[type="submit"]')[0].disabled = true;
  if (checkedOne) {
    document.querySelectorAll('input[type="submit"]')[0].disabled = false;
  }

with

    document.querySelectorAll('input[type="submit"]')[0].disabled = !checkedOne;

 function callFunction() { var checkboxes = document.querySelectorAll('input[type="checkbox"]'); var checkedOne = Array.prototype.slice.call(checkboxes).some(x => x.checked); document.querySelectorAll('input[type="submit"]')[0].disabled = !checkedOne; }
 <input type="checkbox" name="vehicle" onchange="callFunction()" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" onchange="callFunction()" value="Car"> I have a car<br> <input type="submit" value="Submit" disabled>

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

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