How to trigger an event after losing focus from a text box, asp.net mvc

  • I have two text blocks A, B using (Html.textboxfor).
  • TextBox 'A' is enabled, and TextBox 'B' is disabled.
  • When I enter the values ​​in the text box "A" and go outside (moving the cursor out of the text box), TextBox "B" should be filled with some value.

For example: iam entering the value TextBox A = "King", if the focus is lost from the window, the value in the text field B should be "Peter", which should be filled automatically.

I am using asp.net mvc.

+5
source share
5 answers

jsut attrbiute onblur ,

- :

   <script language="javascript">
       function fllB()
       {
          var txtB = document.getElementById("txtB");
          var txtA = document.getElementById("txtA");

          if (txtA.value == "King")
          {
              txtB.value = "Peter";
          }
        }
    </script>

<input type="text" id="txtA" onblur="fillB()" />
<input type="text" id="txtB" />
+3

ASP.NET, .NET, asp: textbox. .

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    if (TextBox1.Text == "King")
    {
        TextBox2.Text = "Peter";
    }
}
+1

javascript, , "".

<script language="javascript">
function validatePrice(textBoxId) {
    var textVal = textBoxId.value;
    var regex = /^(\$|)([1-9]\d{0,2}(\,\d{3})*|([1-9]\d*))(\.\d{2})?$/;
    var passed = textVal.match(regex);
    if (passed == null) {
        alert("Enter price only. For example: 523.36 or $523.36");
        textBoxId.Value = "";
    }
}

, , "onblur", , OnLostFocus(), WinForm.

<asp:TextBox ID="TextBox19" runat="server" Visible="False" Width="183px" 
                onblur="javascript:return validatePrice(this);"></asp:TextBox>
+1

JQuery

<script type="text/javascript">
  $(function()
  {

    $("#txtA").change(function()
    {
      if(this.value == "King") 
        $("#txtB").attr("disabled", "").val( "Peter" );
    });

  });
</script>
0

, , ? , disabled :

<%: Html.TextBoxFor( x => x.XXX, new { id = "txtA" }) %>    
<%: Html.TextBoxFor( x => x.XXX, new { id = "txtB", disabled="true" }) %>

For JavaScript, I assume you are using jquery, which is included in mvc: (If you do not want to clear txtb, if the value is removed from window A, then delete the else statement)

    $(function() 
{
    $("#txtA").change(function()
    {
        if ($("#txtA").val().length > 0) {

            $("#txtB").val(function()
            {
                // Do something to populate #txtb ex: Peter
                var myVar = "Peter";

                return myVar;
            }); 
        }
        else 
        {
            $("#txtB").val("");
        }
    });
});
0
source

All Articles