简体   繁体   English

jQuery keyup函数不起作用

[英]Jquery keyup function not working

In the $(':text').keyup(function() { I tried changing the :text into :number doesn't work only works if I remove the && $('#number').val() != "" $(':text').keyup(function() {我尝试将:text更改为:number,只有在删除&& $('#number').val() != ""

<script>
$(document).ready(function(event) {  
$(':text').keyup(function() {
    if($('#input').val() != "" && $('#number').val() != "" 

    ){
       $('#submit').removeAttr('disabled');
    } else {
       $('#submit').attr('disabled', true);   
    }
});});
</script>

HTML 的HTML

<form><input type=text id='input'>
<input type=number id='number'><br>
<input type=button id='submit' value='submit'>
    </form>

jsfiddle: http://jsfiddle.net/LaLL0v6a/ jsfiddle: http : //jsfiddle.net/LaLL0v6a/

变化 $(':text') $('input[type="text"]')

Use 采用

$('#input').keyup(function() {

or 要么

$('input[type="text"]').keyup(function() {

instead of 代替

$(':text').keyup(function() {

There are 2 problems. 有两个问题。

  1. You are registering the keyup handler to only the first input element, not the second one because its type is number 您正在将keyup处理程序注册到第一个输入元素,而不是第二个,因为它的类型是数字
  2. Use .prop() to set the disabled state 使用.prop()设置禁用状态
  3. If you enter a non numerical value in number field then it will remain disabled because .val() will return an empty string 如果在数字字段中输入非数字值,则它将保持禁用状态,因为.val()将返回一个空字符串

so 所以

 $(document).ready(function (event) { $('#input, #number').keyup(function () { $('#submit').prop('disabled', $('#input').val() == '' || $('#number').val() == ""); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type=text id='input' /> <input type=number id='number' /> <input type=button id='submit' value='submit' disabled /> 

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

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