How Should jquery set the onclick event for changing the button input?

Source: Internet
Author: User

How does jquery set the onclick event of the 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 Discuz! In the same way as in the source code of the NT Forum, all things and scripts on the page are generated by the background code .. then use jquery. load ajax to the page .. others except ie8 cannot be displayed .. other browsers are normal. now the problem is: to dynamically change The onclick function in the previous page button. I tried many methods and did not make them. The following are examples of failure:
1. JavaScript code: Copy codeThe Code is as follows: $ ("# prebt "). unbind ("onclick", function () {var p = $ ("# nextbt "). parent (); $ ("# nextbt "). remove ();
P. append ("<input id = \" nextbt \ "type = \" button \ "value = \" Edit \ "onclick = \" ToPage (14) \ "/> ")});
JavaScript code:
$ ("# Prebt"). removeAttr ("onclick ");
$ ("# Prebt"). attr ("onclick", "ToPage (4 )");
Or:
$ ("# Prebt"). attr ("onclick ","");
$ ("# Prebt"). attr ("onclick", "ToPage (4 )");

2. JavaScript code:Copy codeThe Code is as follows: $ ("# prebt"). removeAttr ("onclick ");
$ ("# Prebt"). click (function () {ToPage (4)}); // This sentence will lead to an endless loop... I don't know what's going on ..
// My paging function... In this way, data is constantly jumping. How can this problem be solved?

There is really no way to do it. I can only post related requests on the Internet. A friend asked me if I want the following results:Copy codeThe Code is as follows: $ ("# prebt"). unbind ("onclick ");
$ ("# Prebt"). bind ("onclick", "ToPage (4 )");

In fact, the effect I want is the same as above, but the above method is similar to my original effect, and cannot be executed, and cannot be bound at all.
Another enthusiastic user gave me a code example, saying it was helpful to me. The details are as follows:
XML/HTML code:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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>
</Head>
<Body>
<Input id = "btndemo" type = "button" value = "click" onclick = "toPage (4)"/>
</Body>
</Html>

During the above test in ie8, the result is 1 when you click. Even so, it is not what I originally wanted. Is there really no way to implement it?
Finally, I came up with a stupid method, which can be implemented as follows:Copy codeThe Code is as follows: <span id = "prespan"> <input id = "prebt" type = "button" value = "Previous Page" onclick = "ToPage (2) "/> </span>

Then the background program automatically replaces the content in prespan.Copy codeThe Code is as follows: $ ('# prespan'). empty ();
$ ('# Prespan '). append ("<input id = \" prebt \ "type = \" button \ "value = \" Previous Page \ "onclick =" \ ToPage (4) \ "/> ");

Although onclick is not used, the problem is solved.

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.