zoukankan      html  css  js  c++  java
  • jQuery为动态生成的select元素添加事件

     
    项目中需要在点击按钮时动态生成select元素,为防止每次点击按钮时从服务器端获取数据(因为数据都是相同的),可以这样写代码
    1、首先定义全局js变量
    var strVoucherGroupSelect ="";
    2、在js中写好获取服务端数据的代码
    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 需要动态增加select的时候,可以这样写
    $("#divID").append(genVoucherGroupSelect(rowID) );

    5 给select增加点击事件,在第四步后增加
    $("#sl_0" + rowID).bind("onchange", function(){
          alert("你点击了下拉框");
    })

  • 相关阅读:
    IDEA连接Spark集群执行Scala程序
    win10安装mysql,及重装
    python生产和消费kafka数据
    protobuf 协议浅析
    操作系统-第十三章-I/O系统
    操作系统-第十二章-大容量存储结构
    操作系统-第十一章-文件系统的实现
    JSONP跨域提交请求
    标识多个物体并返回物体中心坐标方法的实现
    SkyWalking Agent端日志插件的编写历程与使用说明
  • 原文地址:https://www.cnblogs.com/lvlin/p/1536098.html
Copyright © 2011-2022 走看看