wuruize888 2017-05-25 07:23 采纳率: 66.7%
浏览 939
已采纳

jQuery,后加元素问题,动作都不管用,怎么搞?

$(function () {

$("#newpren, #oldpren").click(function () {        //用这个创建的input元素,在minput函数里不管用怎么?其它的原始input元素都可以驱动那个minput函数。
        if ($(this).attr("id") === "newpren") {
            $("#newinput").empty();
            $("#newinput").append("<input id=\"newclass\" name=\"newclass\" style=\"margin-top:10px;\" mtype=\"str:4:30\" class=\"form-control\" placeholder=\"新父类名称\" min=\"5\" max=\"30\" type=\"text\"/><span></span>");
        } else if ($(this).attr("id") === "oldpren") {
            $("#newinput").empty();
            $("#newinput").append("<input id=\"oldclass\" name=\"oldclass\" style=\"margin-top:10px;\" mtype=\"str:4:30\" placeholder=\"旧子类列表\" class=\"form-control\" min=\"5\" max=\"30\" type=\"text\"/><span></span>");
        }
    });


         $('form :input').keyup(function () {
        MINPUT($(this));                
    });

        function MINPUT(typeS) {
        alert("生效!");
        }
  });                   
                    <div>
                    <button type="button" id="newpren">使用新父类</button>
                    <button type="button" id="oldpren">使用已有子类</button>
                </div>


  • 写回答

4条回答

  • Go 旅城通票 2017-05-25 08:02
    关注

    因为执行 $('form :input').keyup(function () {这个绑定的时候只绑定dom中已经存在的,你事件添加生成的再这个代码执行之后,没在dom树种所以没有绑定事件,重新绑定过就行了,如果你用的1.7+的jquery可以改为这样,用代理对动态生成的内容也有效

             $('form').on('keyup',':input',function () {
                MINPUT($(this));
            });
    

    1.6-的找到元素重新绑定一次事件

    
            $("#newpren, #oldpren").click(function () {        //用这个创建的input元素,在minput函数里不管用怎么?其它的原始input元素都可以驱动那个minput函数。
                if ($(this).attr("id") === "newpren") {
                    $("#newinput").empty();
                    $("#newinput").append("<input id=\"newclass\" name=\"newclass\" style=\"margin-top:10px;\" mtype=\"str:4:30\" class=\"form-control\" placeholder=\"新父类名称\" min=\"5\" max=\"30\" type=\"text\" /><span></span>")
                     .find('input').keyup(function () { MINPUT($(this)); });
                } else if ($(this).attr("id") === "oldpren") {
                    $("#newinput").empty();
                    $("#newinput").append("<input id=\"oldclass\" name=\"oldclass\" style=\"margin-top:10px;\" mtype=\"str:4:30\" placeholder=\"旧子类列表\" class=\"form-control\" min=\"5\" max=\"30\" type=\"text\" /><span></span>")
                    .find('input').keyup(function () { MINPUT($(this)); });
                }
            });
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序