How jquery should set the OnClick event _ Basics to Change button input

Source: Internet
Author: User
How should jquery set the OnClick event for button input? For example, there is a button at the beginning: <input id= "prebt" type= "button" value= "previous page" onclick= "ToPage (2)"/> This line of code is generated in the background of the program, and the discuz! NT Forum source code in the same way, all the things on the page, along with the script is generated by the background codes. Then load the page with Jquery.ajax. Other than the IE8 can not be displayed outside. Other browsers are normal. Now the question is: to dynamically change the function of the onclick button in the previous page. I tried a lot of different ways, I didn't do it, and here are a few examples of failures:
1.JavaScript Code:
Copy Code code as follows:

$ ("#prebt"). Unbind ("onclick", function () {var p= $ ("#nextbt"). Parent (); $ ("#nextbt"). Remove ();
P.append ("<input id=\" nextbt\ "type=\" button\ "value=\" edit \ "Onclick=\" topage () \ "/>")});
JavaScript Code:
$ ("#prebt"). Removeattr ("onclick");
$ ("#prebt"). attr ("onclick", "ToPage (4)");
Or:
$ ("#prebt"). attr ("onclick", "");
$ ("#prebt"). attr ("onclick", "ToPage (4)");

2.JavaScript Code:
Copy Code code as follows:

$ ("#prebt"). Removeattr ("onclick");
$ ("#prebt"). Click (function () {ToPage (4)}); This sentence can lead to a dead loop. I don't know what's going on.
I do the paging function ... This writing results in the data has been jumping, how to solve it?

There is no way, can only be published on the Internet related requests, a friend asked me whether I want the effect as follows:
Copy Code code as follows:

$ ("#prebt"). Unbind ("onclick");
$ ("#prebt"). Bind ("onclick", "ToPage (4)");

In fact, I want the effect is the same as above, but the above method and my original effect is similar, but also no way to carry out, and is not bound up.
There are enthusiastic netizens gave me a code example, said to be helpful to me, such as the following details:
Xml/html Code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" ></ Script>
<script type= "Text/javascript" >
<! [cdata[
function ToPage (ID) {alert (ID);}
$ (function () {
$ (' #btndemo '). Removeattr (' onclick '). Unbind (' click '). Click (function () {
ToPage (1);
})
})
]]>
</script>
<body>
<input id= "Btndemo" type= "button" value= "click" onclick= "ToPage (4)"/>
</body>

When the above test in IE8, the result is 1, even if it is, but it is not the effect I originally wanted. Is it true that there is no way to achieve it?
Finally, I have come up with a relatively stupid method, can be achieved, as follows:
Copy Code code as follows:

<span id= "Prespan" ><input id= "prebt" type= "button" value= "previous page" onclick= "ToPage (2)"/></span>

The background program then automatically replaces the contents of the Prespan.
Copy Code code as follows:

$ (' #prespan '). empty ();
$ (' #prespan '). Append ("<input id=\" prebt\ "type=\" button\ "value=\" previous page \ "onclick=" \topage (4) \ "/>");

Although it is not used to set the onclick. But in the end it solved my problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.