jQuery重新加载DOM? [英] jQuery reload the DOM?

查看:1136
本文介绍了jQuery重新加载DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面上有一个按钮。当用户点击按钮时,它会动态渲染一个表单(即它不只是显示一个隐藏的表单..它完全使用jQuery创建它)。



我的问题是新的创建的表单不响应任何jQuery命令。这是我现在提供的表单的代码。

  $(#savenewlang)。click(function e){
console.log(savenewlang已被点击);
});

所以当他们点击提交按钮但是没有运行时,应该只是console.log。 >

任何想法如何重新加载DOM或分配一个正确触发的实际事件?

解决方案

  $(#container)。on('click','#savenewlang',function(e){
console.log(savenewlang has been clicked );
});

这里 #container 指向父元素的 #savenewlang 在页面加载时属于DOM。






要指定此事件 .on()您需要 三个参数

  .on(eventName,target,callback); 

但是对于普通绑定,只需要 两个参数 strong>

  .on(eventName,callback); 

详细了解 .on()






剩余



将所有代码放在

 code> $(document).ready(function(){

});

简而言之

 code> $(function(){

});


I have a page that has a button on it. When the user clicks the button it dynamically render's a form (ie it's not just showing a hidden form.. it's completely creating it using jQuery).

My issue is the newly created form doesn't respond to any jQuery commands. This is the code I have for the rendered form at the moment.

$("#savenewlang").click(function(e) {
    console.log("savenewlang has been clicked");
});

So it should just console.log when they click the submit button but it's not running.

Any idea how to reload the DOM or assign that an actual event that correctly fires?

解决方案

$("#container").on('click', '#savenewlang', function(e) {
    console.log("savenewlang has been clicked");
});

Here #container points to a parent element of #savenewlang that belongs to DOM at page load.


To specify this event .on() you need three arguments

.on(eventName, target, callback);

But for ordinary binding it only needs two arguments

.on(eventName, callback);

Read more about .on()


Remainder

Put all of your code within

$(document).ready(function() {

});

in short

$(function() {

});

这篇关于jQuery重新加载DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆