$(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>
jQuery,后加元素问题,动作都不管用,怎么搞?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
斯洛文尼亚旅游 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)); }); } });本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报