事件监听器.change没有响应

时间:2017-02-16 01:22:14

标签: jquery

我有一个输入框,用户在其中键入电子邮件。如果输入是!= 0,我希望我的脚本在我的DOM中禁用一个按钮。但是,如果它改变并且值变为> 0,我希望它启用我的按钮。

不幸的是没有任何事情发生。

这是我到目前为止所做的:

<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="Example@Example.com", type="email"})
<button disabled type="submit" class="btn btn-default" style="display:inline-block" id="emailSignup">Tilmeld</button>

2 个答案:

答案 0 :(得分:0)

你能试试这个剧本吗?谢谢。 您必须定义变量然后测试函数。

   @Html.TextBoxFor(Model => Model.Email, new { id="input1", Class = "form-control", Style = "display:inline-block; max-width:200px", Placeholder="Example@Example.com", 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);
    })
});

答案 1 :(得分: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>
相关问题