简体   繁体   中英

Need JavaScript syntax correction

I am a newbie to JavaScript and I coded the following small JavaScript to make a postback function and its not working. I need anybody to correct it please.

C# code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox4.Text = "nopost";
        }
        else
        {
            TextBox4.Text = "post";
        }
    }

JavaScript:

    <script type="text/javascript">
        function a1(){
        var hid = document.getElementById('TextBox4').Value;
        if (hid == "post") {
            alert('Posted');
        }
        else if (hid == "nopost") {
        alert('Not Posted');
        }
        }
        window.onload == function () {
            a1();
        }
    </script>

ASP code

    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

尝试使用ClientID属性并将.Value更改为.value

var hid = document.getElementById('<%= TextBox4.ClientID %>').value;

You're using == instead of = for the onload handler assignment.

This...

window.onload == function () {
    a1();
}

should be this...

window.onload = function () {
    a1();
};

or simply this...

window.onload = a1;

the problem might be with how you have defined the onload function change it to:

window.onload = a1;

PS: Use the === operator in Javascript for type safe comparison.

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