开发者

How to send an asp control to javascript

I am trying to send an asp control (textbox) to a javascript function.

onblur="CalculateLossRatio(this.val开发者_高级运维ue,<%=txtLossRatioCurrentYear.ClientID%>)"

Is is the right way to do this.


In your situation you could do something like the following:

<asp:TextBox ID="TextBox1" onblur="CalculateLossRatio(this.value, 1)" runat="server" />
<asp:TextBox ID="TextBox2" runat="server" Text="7"/>

<script type="text/javascript">

function CalculateLossRatio(arg1, arg2)
{
    if (arg2 == 1)
    {
        var txt = document.getElementById('<%=TextBox2.ClientID%>');
    }
    else if(arg2 == 2)
    {
        // TODO - get other txt...
    }

    alert(arg1 - txt.value);
}

</script>


thats is acceptable, what you are actually doing there is using a preprocessor directive

<%=txtLossRatioCurrentYear.ClientID%> will be substitute on runtime by the generated ID of the control, passing this.value will pass the reference of the control but you can still obtain it using the var txtbox = document.getElementById(controlid);


You can send the reference to the sender directly to your js-function(its not clear from question if there is another Textbox):

onblur="CalculateLossRatio(this)"

function CalculateLossRatio(txtBox)
{
    if(txtBox != null){
       var text=txtBox.value;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜