简体   繁体   中英

on focus not working in asp textbox

On entering rate and quantity I want the total amount to be calculated automatically, so I tried the following.

function getAmt()
{   
    if(isNaN(document.getElementById('<%=TxtRate.ClientID%>').value))
    {
        alert("Illegal Rate Entered, Please Check It Out.");
        document.getElementById('<%=TxtRate.ClientID%>').focus;

        return;
    }
    else if(isNaN(document.getElementById('<%=TxtQty.ClientID%>').value))
    {
        alert("Illegal Quantity Entered, Please Check It Out.");
        document.getElementById('<%=TxtQty.ClientID%>').focus;

        return;
    }   
    document.getElementById('<%=TxtAmt.ClientID%>').value=parseFloat
        (document.getElementById('<%=TxtRate.ClientID%>').value)*parseFloat
        (document.getElementById('<%=TxtQty.ClientID%>').value)
}

Now I created a text box:

<TextBox ID="TxtAmt" runat="server" BorderStyle="Solid" onFocus="getAmt()">

This code is not running.. what am i missing ??

Try this:

var amtText = $('#' + '<%=TxtAmt.ClientID%>');
amtText.bind('focus', getAmt()); // using jQuery here

If you don't want use jQuery (or any other JavaScript library), then you have to user pure JavaScript. However to attach a handler (an event listener) to an event in the browser, you have 2 ways:

  1. You can use the onevent attribute of the HTML element (like onclick , or onfocus , etc.), which is commonly known as DOM Events Level 1
  2. Or you can use addEventListener and removeEventListeners of DOM Level 2 Events , or attachEvent and detachEvent in IE8- browsers. For more information and samples about it, see this MDN page.

Try it out

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "foc", "document.getElementById('" + TxtAmt.ClientID + "').focus();", true);

Use above code segment when you want to focus the txt amt and once the txtamt is focus your function will calculate the amount value based on the provided rate and quantity..

If you find it useful, please mark it as your answer else let me know...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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