Javascript无法识别创建的按钮
Javascript not recognizing a created button
我有一些Javascript,当ajax表单提交成功时,它会创建一个按钮。重新加载页面时,按钮将呈现到页面上。
它是一个简单的javascript组合形式,用于向表中添加一个项,并使用remove按钮删除该项。
我运行了一个diff工具,提交表单和创建按钮以及用已经创建的按钮重新加载页面之间没有任何区别。这让我相信Javascript无法识别正在创建的按钮。
这里有一些代码:
这是我的javascript方法
$('button#proxy_remove_given').click(function() {
$.ajax({
url: $('button#proxy_remove_given').attr('action'),
type: 'POST',
data: $('form#proxy_submit_form').serialize(),
success: function(responce) {
if ("Success" == responce) {
var username = $('button#proxy_remove_given').attr('name');
$('#given_proxy_access_table tr#'+username).remove();
var table_length = document.getElementById("given_proxy_access_table").rows.length;
if (table_length == 0) {
var table = document.getElementById("given_proxy_access_table");
var new_row = table.insertRow(0);
new_row.id = "NoProxyRow";
var cell1 = new_row.insertCell(0);
var cell2 = new_row.insertCell(1);
cell1.innerHTML = "<p>No Proxies Found</p>";
cell2.innerHTML = "<button data-toggle='"collapse'" data-target='"#add'">Add</button>";
}
}
}
});
});
以下是添加按钮的javascript
$('button#proxy_submit').click(function() {
$.ajax({
url:'proxy/submit',
type: 'POST',
data: $('form#proxy_submit_form').serialize(),
success: function(responce) {
if ("Success" == responce) {
var table = document.getElementById("given_proxy_access_table");
if (table.rows.length == 1) {
if (table.rows[0].id == "NoProxyRow") {
document.getElementById("given_proxy_access_table").deleteRow(0);
}
}
var username = document.getElementById('id_proxy_username').value
var new_row = table.insertRow();
new_row.id = username;
var cell1 = new_row.insertCell(0);
var cell2 = new_row.insertCell(1);
cell1.innerHTML = "<p>{0}</p>".replace(/'{0'}/g,username);
cell2.innerHTML = "<button type='"submit'" name='"{0}'" id='"proxy_remove_given'" action='"proxy/remove/given/{0}'">Remove</button>".replace(/'{0'}/g,username);
document.getElementById("proxy_submit_form").reset();
}
}
});
});
知道javascript为什么不能识别正在创建的按钮吗?
<button type="submit" name="MikeC" id="proxy_remove_given" action="proxy/remove/given/MikeC">Remove</button>
编辑:
为什么这个
$('button#proxy_remove_given').click(function()
拿起这个
<button type="submit" name="MikeC" id="proxy_remove_given" action="proxy/remove/given/MikeC">Remove</button>
当我将按钮添加到页面时
但当我重新加载页面时,它会接收按钮调用
这在某种程度上取决于代码的结构,但我猜你的点击监听器找不到按钮,因为它还不存在。
$('button#proxy_remove_given')
寻找按钮,但它还没有找到。您应该在添加按钮之后放置您的点击监听器。
如果我得到你的点
第一个:如果您使用提交表单或按钮类型的提交点击而不重新加载页面,您可以使用e.preventDefault()
第二:对于动态生成的元素,您应该使用
$('body').on('click', 'selector' , function(){});
看看动态创建的元素上的事件绑定?
相关文章:
- 如何删除我用按钮创建的内容
- 单击按钮创建多实例jQuery UI对话框
- 如何为滑块的分页按钮创建简单的下一个和上一个触发器按钮
- Javascript使用变量通过按钮创建url
- 按钮创建无法定位功能代码错误
- 使用按钮创建新的下拉列表
- 点击按钮创建一个带有css动画的放射状进度条
- 从执行jscript列表按钮创建多个相关对象
- 在完整日历的Dayclick事件上触发jQuery qTip,然后使用其中的按钮创建事件
- 通过摇晃按钮创建小摆动
- 摆脱形式,仅使用按钮创建新记录
- 如何使用下一个和上一个按钮创建 jquery 滑块
- Chrome 扩展程序:通过单击按钮创建新标签页
- 使用按钮创建列表
- ExtJS:如何使用MessageBox按钮创建自定义模式窗口
- 如何在ASP.Net中单击按钮创建动态表
- carouFredSel:为next/prev按钮创建一个函数
- extjs如何使用单选按钮创建菜单
- 单击按钮创建表时,请选中“获取值”复选框
- 如何使按钮创建一个不同的文本,每次它是按下