简体   繁体   English

当输入字段为空时如何禁用按钮?

[英]How to disable a button when an input field is empty?

I tried this but it's not working.我试过这个,但它不起作用。

 $('#booking').attr('disabled', true); $('input:text').keyup(function() { var disable = false; $('input:text').each(function() { if ($(this).val() == "") { disable = true; } }); $('#booking').prop('disabled', disable); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="<?php echo site_url('home/program/booking');?>" method="post"> <div class="form-group"> <label class="col-sm-2 control-label">Tour </label> <div class="col-sm-10"> <input class="form-control" id="tour" name="tour" type="text"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Firstname </label> <div class="col-sm-10"> <input class="form-control" id="firstname" type="text" value=""> </div> </div> <button id="booking" type="submit" class="btn btn-default">OK</button> </form>

You should use disable='disabled' and disabled='' instead of true / false , though using jQuery it will work with both您应该使用disable='disabled'disabled=''而不是true / false ,尽管使用 jQuery 它可以同时使用

And with jQuery you should use .prop instead of .attr使用 jQuery 你应该使用.prop而不是.attr

\n

 $('#booking').attr('disabled', 'disabled'); $('input').keyup(function() { var d = ''; $('input').each(function() { if ($(this).val() == "") { d = 'disabled'; } }); $('#booking').prop('disabled', d); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="<?php echo site_url('home/program/booking');?>" method="post"> <div class="form-group"> <label class="col-sm-2 control-label">Tour</label> <div class="col-sm-10"> <input class="form-control" id="tour" name="tour" type="text"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Firstname</label> <div class="col-sm-10"> <input class="form-control" id="firstname" type="text" value=""> </div> </div> <button id="booking" type="submit" class="btn btn-default">OK</button> </form>
\n

Update更新

Did you add the jQuery library to your solution?您是否将 jQuery 库添加到您的解决方案中? ... If I do that on your existing code it actually does work as is, as you can see in below sample ...如果我在您现有的代码上这样做,它实际上可以按原样工作,如下面的示例所示

 $('#booking').prop('disabled', true); $('input:text').keyup(function() { var disable = false; $('input:text').each(function() { if ($(this).val() == "") { disable = true; } }); $('#booking').prop('disabled', disable); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="<?php echo site_url('home/program/booking');?>" method="post"> <div class="form-group"> <label class="col-sm-2 control-label">Tour</label> <div class="col-sm-10"> <input class="form-control" id="tour" name="tour" type="text"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Firstname</label> <div class="col-sm-10"> <input class="form-control" id="firstname" type="text" value=""> </div> </div> <button id="booking" type="submit" class="btn btn-default">OK</button> </form>

You can use return in your function once when it hits a input box with an empty value instead of keep looking through and changing the that disable variable你可以在你的函数中使用return一次,当它遇到一个空值的输入框时,而不是继续查看并更改那个disable变量

 $('#booking').attr('disabled', true); $('input').keyup(function() { var disable = false; $('input').each(function() { if ($(this).val() == "") { $('#booking').attr('disabled', true); return } else { $('#booking').attr('disabled', false); } }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="<?php echo site_url('home/program/booking');?>" method="post"> <div class="form-group"> <label class="col-sm-2 control-label">Tour</label> <div class="col-sm-10"> <input class="form-control" id="tour" name="tour" type="text"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Firstname</label> <div class="col-sm-10"> <input class="form-control" id="firstname" type="text" value=""> </div> </div> <button id="booking" type="submit" class="btn btn-default">OK</button>

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

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