2013-11-27 73 views
0

我正在使用Jquery檢查輸入是否具有特定值,如果它具有該值,它將啓用提交按鈕。問題是我將值設置爲4,但如果輸入44(或以4開頭的任何內容),它仍然啓用該按鈕。同樣一旦進入4,它可以改變爲任何東西,並且按鈕保持啓用狀態。檢查輸入是否具有特定值

我希望它做的只是改變爲啓用,如果值是4,如果值改變,那麼提交按鈕應該被禁用。

jQuery的

$(document).ready(function() { 
    $('#check').keyup(function() { 
     if($(this).val() === '4') { 
      $('.submit').removeAttr('disabled'); 
     } 
    }); 
}); 

HTML

<input id="check" type="text" name="check" /> 

<input type="submit" name="submit" class="submit" disabled="disabled"> 

回答

3

試試這個:其實

$('#check').change(function() { 
    if($(this).val() === '4') { 
     $('.submit').removeAttr('disabled'); 
    } 
    else $('.submit').attr('disabled', 'disabled'); 
}); 

你需要重新禁用提交按鈕,如果你願意的話,當值不4

更重要的是,代替

$('.submit').attr('disabled', 'disabled'); 

你可以/應該使用

$('.submit').prop('disabled', true); 

所以處理變得

$('#check').change(function() { 
    if($(this).val() === '4') { 
     $('.submit').removeAttr('disabled'); 
    } 
    else $('.submit').prop('disabled', true); 
}); 
0

它的發生,因爲你沒有禁用按鈕,如果值是不是4.

$('#check').keyup(function() { 
    if($(this).val() === '4') { 
     $('.submit').removeAttr('disabled'); 
    } 
    else{ 
     $('.submit').attr('disabled','disabled'); 
    } 
}); 
0

只需添加禁用它:)

$(document).ready(function() { 
    $('#check').keyup(function() { 
     if($(this).val() === '4') { 
      $('.submit').removeAttr('disabled'); 
     } else { 
      $('.submit').prop('disabled', true); 
     } 
    }); 
}); 
0

你需要redisable它的人。

$(document).ready(function() { 
    $('#check').change(function() { 
     if($(this).val() === '4') { 
      $('.submit').removeAttr('disabled'); 
     }else{ 
      $('.submit').prop('disabled'); 
     } 
}); 
0

用途:

$('#check').keyup(function() { 
    $('.submit').prop('disabled', $(this).val() !== '4'); 
}); 

jsFiddle example