JQuery adds an event to a dynamically generated select Element

Source: Internet
Author: User
The following small series will provide you with a method for jQuery to add events to the dynamically generated select element. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the small editor. You need to dynamically generate the select element when you click the button in the project. To prevent data from being retrieved from the server every time you click the button (because the data is the same), you can write code like this.

1. First define global js Variables

Var strVoucherGroupSelect = "";

2. Write the code for obtaining server data in js.

Function genVoucherGroupSelect (rowID) {return $ (strVoucherGroupSelect ). attr ("id", "sl _" + rowid0000.parent().html (); // return the complete html in the drop-down box after adding the ID} function getVoucherGroupData () {$. ajax ({type: "Post", url: "/BillWeb/OrgVoucher/GetVoucherGroup", dataType: "json", data: "", cache: true, success: function (res) {var str = $ (""); Var option =" "; for (var j = 0; j <res. length; j ++) {option + =""+ Res [j]. Text +"";}Strvouchergroupselect = certificate (str;.html(option;.parent().html ();}});}

3. Compile the initialization code on the page.

$().ready(function(){    getVoucherGroupData();  });

4. When you need to dynamically Add a select statement, you can write it like this.

$("#pID").append(genVoucherGroupSelect(rowID) );

5. Add a click event to the select statement.

$ ("# Sl_0" + rowID). bind ("change", function () {alert ("You clicked the drop-down box ");})

The above jQuery Method for adding events to the dynamically generated select element is all the content shared by the editor. I hope to give you a reference and support for PHP.

For more information about how jQuery adds events to dynamically generated select elements, see PHP!

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.