| |

VerySource

 Forgot password?
 Register
Search
View: 932|Reply: 6

Error adding ONCLICK event to button

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 LAN

Post time: 2020-1-10 18:20:01
| Show all posts |Read mode
.cs file
private void Page_Load (object sender, System.EventArgs e)
{
btnLogin.Attributes.Add ("onclick", "checkUserName ();");
}

html code:
<script language = "javascript" src = "ScriptFiles / BBS_JScript.js"> </ script>


Code in BBS_JScript.js:

function checkUserName ()
{
var isRight;
var userName = document.all ("txtUserName"). value;
var userNameLen = userName.length;
if (userName == "")
    {
       alert ("The username cannot be empty!");
       isRight = false;
    }
    else
    {
if (userNameLen> 6&&userNameLen <= 16)
{
isRight = true;
}
else
{
alert ("The username is between 6 and 16 characters long!");
isRight = false;
}
}
    return isRight;
}

"Runtime error" and "Syntax error" occur when the page loads.
I put
else
{
alert ("The username is between 6 and 16 characters long!");
isRight = false;
}
There will be no problem if you delete it, I really do n’t understand why, please master, thank you
Reply

Use magic Report

0

Threads

13

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-13 14:00:01
| Show all posts
Give it a try
btnLogin.Attributes.Add ("onclick", "return checkUserName ();");
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-13 17:18:01
| Show all posts
alert ("The username is between 6 and 16 characters long!");
to
alert ('The username is between 6 and 16 characters in length!');
Try
Reply

Use magic Report

3

Threads

29

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-1-14 11:00:02
| Show all posts
Bobo was right. In addition, you can write return checkUserName (); directly to the OnClientClick property of that control.
Reply

Use magic Report

1

Threads

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 China

Post time: 2020-1-16 20:54:01
| Show all posts
You don't return this function
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-17 08:54:01
| Show all posts
Renamed
btnLogin.Attributes.Add ("onclick", "return checkUserName ();");
It's ok
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-17 10:36:02
| Show all posts
Learn
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list