欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery為動(dòng)態(tài)生成的select元素添加事件的方法

 更新時(shí)間:2016年08月29日 10:03:26   投稿:jingxian  
下面小編就為大家?guī)硪黄猨Query為動(dòng)態(tài)生成的select元素添加事件的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

項(xiàng)目中需要在點(diǎn)擊按鈕時(shí)動(dòng)態(tài)生成select元素,為防止每次點(diǎn)擊按鈕時(shí)從服務(wù)器端獲取數(shù)據(jù)(因?yàn)閿?shù)據(jù)都是相同的),可以這樣寫代碼

1、首先定義全局js變量

var strVoucherGroupSelect ="";

2、在js中寫好獲取服務(wù)端數(shù)據(jù)的代碼

function genVoucherGroupSelect(rowID){
  return $(strVoucherGroupSelect).attr("id", "sl_" + rowID).parent().html();  //返回增加ID后的下拉框完整html
}
function getVoucherGroupData(){
  $.ajax({
    type: "Post",
    url: "/BillWeb/OrgVoucher/GetVoucherGroup",
    dataType: "json",
    data: "",
    cache: true,
    success: function(res) {
        var str = $("<select></select>");
        var option = "";
        for(var j =0;j < res.length; j++)
        {
          option += "<option value=\"" + res[j].Value + "\">" + res[j].Text + "</option>";
        }
        strVoucherGroupSelect = $(str).html(option).parent().html();
    }
  });
}

3 在頁面中編寫初始化代碼

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

4 需要?jiǎng)討B(tài)增加select的時(shí)候,可以這樣寫

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

5 給select增加點(diǎn)擊事件,在第四步后增加

$("#sl_0" + rowID).bind("change", function(){
   alert("你點(diǎn)擊了下拉框");
})

以上這篇jQuery為動(dòng)態(tài)生成的select元素添加事件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論