2017-02-16 181 views
0

我有一個用戶輸入電子郵件的輸入框。如果輸入是!= 0,我希望我的腳本在我的DOM中禁用按鈕。但是,如果它發生變化並且值變爲> 0,我希望它啓用我的按鈕。事件監聽器.change沒有響應

不幸的是沒有任何反應。

這是我到目前爲止有:

<script src="~/Scripts/jquery-3.1.1.js"> 
    $(document).ready(function(){ 
     $("#input1").change(function(){ 
      if($(this).val().length > 0){ 
       $("#emailSignup").prop('disabled', false); 
      } 
      else{ 
       $("#emailSignup").prop('disabled', true); 
      } 
     }) 
    }); 
</script> 

HTML:

@Html.TextBoxFor(Model => Model.Email, new { id="input1", Class = "form-control", Style = "display:inline-block; max-width:200px", Placeholder="[email protected]", type="email"}) 
<button disabled type="submit" class="btn btn-default" style="display:inline-block" id="emailSignup">Tilmeld</button> 
+0

是動態添加的輸入,如果這樣使用事件deledation – guradio

+0

它是動態添加是的,不是嗎?回覆 –

+0

你看看控制檯日誌嗎?也許這裏有一些錯誤信息。 –

回答

0

你可以重點用起來反而可能:

<script src="~/Scripts/jquery-3.1.1.js"> 
    $(document).on('keyup', '#input1', function() { 
     if($(this).val().length > 0){ 
      $("#emailSignup").removeAttr('disabled'); 
     } else { 
      $("#emailSignup").prop('disabled', true); 
     } 
    }); 
</script> 
0

難道你試試這個腳本?謝謝。 您必須定義變量,然後測試該功能。

@Html.TextBoxFor(Model => Model.Email, new { id="input1", Class = "form-control", Style = "display:inline-block; max-width:200px", Placeholder="[email protected]", type="email"}) 
<button disabled="disabled" type="submit" class="btn btn-default" style="display:inline-block" id="emailSignup">Tilmeld</button> 

的jQuery:

$(document).ready(function(){ 
    $('.emailSignup').attr('disabled',true); 
    $('#message').keyup(function(){ 
     if($(this).val().length !=0) 
      $('.emailSignup').attr('disabled', false);    
     else 
      $('.emailSignup').attr('disabled',true); 
    }) 
}); 
+0

這不應該有任何不同。 - 它不起作用,不幸的是 –

+0

@JeppeChristensen,你可以分享#input1的HTML代碼嗎? – FreedomPride

+0

我更新了我的問題:-) –