使用包含的 js 文件触发提交功能的问题
Issue with triggering the submit function with included js file
我在触发表单提交时遇到问题。我使用页面 header 中包含的 js 文件将表单设置为 <div id="access"></div>
innerHTML。作为:
<div id="access">
<form method="post" id="acc">
<input type="text">
<input type="submit">
</form>
</div>
表单提交在 header 中包含的 js 文件中实现,但他们没有监听我的提交功能,该功能实现为:
$('#acc').submit(function(){
alert("dsds");
return false;
});
相同的 js 函数在同一个文件中也不起作用。我怎样才能让它听取上面的 lead_form
提交?
因为jQuery的选择器只作用于运行时页面上存在的元素。
而是使用 jQuery.on() 函数。
$('#access').on('submit', '#acc', function() { /* Your code here. */ });
编辑 澄清一下:'#access' 存在于 运行 时间,因此您可以将事件绑定到它,然后引用它的子项元素“#acc”。请记住,尽管使用了 $('#access') 选择器,但在匿名函数中 'this' 的上下文将引用“#acc”而不是“#access”。
我在触发表单提交时遇到问题。我使用页面 header 中包含的 js 文件将表单设置为 <div id="access"></div>
innerHTML。作为:
<div id="access">
<form method="post" id="acc">
<input type="text">
<input type="submit">
</form>
</div>
表单提交在 header 中包含的 js 文件中实现,但他们没有监听我的提交功能,该功能实现为:
$('#acc').submit(function(){
alert("dsds");
return false;
});
相同的 js 函数在同一个文件中也不起作用。我怎样才能让它听取上面的 lead_form
提交?
因为jQuery的选择器只作用于运行时页面上存在的元素。
而是使用 jQuery.on() 函数。
$('#access').on('submit', '#acc', function() { /* Your code here. */ });
编辑 澄清一下:'#access' 存在于 运行 时间,因此您可以将事件绑定到它,然后引用它的子项元素“#acc”。请记住,尽管使用了 $('#access') 选择器,但在匿名函数中 'this' 的上下文将引用“#acc”而不是“#access”。