简体   繁体   English

@ Html.TextBoxFor文本更改事件

[英]@Html.TextBoxFor text changed event

I have an @Html.TextBoxFor as below : 我有一个@Html.TextBoxFor如下:

@Html.TextBoxFor(u => u.SomeProperty, new { @class = "jtb", @id = "TrajName", placeholder = "Traj Name" })

Now what I want to achieve is I want to know when a new character is added to this textbox at the same time it is entered. 现在我想要实现的是我想知道何时在输入的同时将新字符添加到此文本框中。 I want to change a button state to active at this time. 我想在此时将按钮状态更改为活动状态。 I tried blur and change event. 我试过blurchange事件。 But it is fired only when the focus changes. 但只有在焦点发生变化时才会触发它。

$(document).ready(function () {
    $('#TrajName').val("");
    $('#StartLocation').val("-Select Location-");
    $('#EndLocation').val("-Select Location-");

    document.getElementById("BtnAddTraj").disabled = true;

    $("#TrajectoryName").blur(function () {
        if ($('#TrajName').text != "")
            document.getElementById("BtnAddTraj").disabled = false;
        else
            document.getElementById("BtnAddTraj").disabled = true;
    });
});

I have a scenario where user can directly try to click on the button after entering some text(ie cursor still inside textbox and focus is not changed). 我有一个场景,用户可以在输入一些文本后直接尝试点击按钮(即光标仍在文本框内,焦点不会改变)。

So is there any event that gives live trigger when a character is added instead of firing on focus change? 那么当添加一个角色而不是在焦点变化时触发时,是否有任何事件可以提供实时触发?

You need to bind an event handler to the keyup JavaScript event. 您需要将事件处理程序绑定到keyup JavaScript事件。

Further more, I recommend you to use .prop for set disabled property. 此外,我建议您使用.prop来设置禁用属性。

Please try this: 请试试这个:

@Html.TextBoxFor(u => u.SomeProperty, new { @class = "jtb", @id = "TrajName", placeholder = "Traj Name" }) 

 $("#TrajectoryName").keyup(function () {
    if ($('#TrajName').val() != "")
        $("#BtnAddTraj").prop('disabled',false);
    else
        $("#BtnAddTraj").prop('disabled',true);
});

Another solution is to trigger the input event to the TrajectoryName textbox. 另一种解决方案是将input事件触发到TrajectoryName文本框。 This would fire every time your input changes. 每次输入更改时都会触发。

$("#TrajectoryName").on('input',function () {
    if ($('#TrajName').val() != "")
        $("#BtnAddTraj").prop('disabled',false);
    else
        $("#BtnAddTraj").prop('disabled',true);
});

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

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