[英]Javascript/jquery .click() not triggering function
I have a table where some functions are triggered upon clicks on a link in each row:我有一个表格,其中在点击每一行中的链接时会触发一些功能:
The relevant part of the code is:代码的相关部分是:
$('.my_table').inplacerowedit({
url: myurl,
});
in inplacerowedit.js file, I have:在 inplacerowedit.js 文件中,我有:
(function($) {
$.fn.inplacerowedit = function(options) {
var ops = $.extend({}, $.fn.inplacerowedit.defaults, options);
$(this).find(ops.editbuttonselector).on('click', function(e) {
... }
ops.editbuttonselector = 'a.edit'
My table has an edit link each row and it's usually working fine.我的表格每行都有一个编辑链接,它通常工作正常。 My problem is with new created rows.
我的问题是新创建的行。
Here is how I'm creating a row and adding it to the table:这是我创建一行并将其添加到表中的方法:
new_row = null
getNewRow = function() {
if (new_row == null){
new_row = $("<tr>");
columns = {'name':'','type':'','value':'','edit':'','delete':''}
for (var column in columns)
new_row.append( $("<td>").addClass(column).text(columns[column]));
links = ['edit','delete']
for (var i=0;i<links.length;i++){
link = links[i]
a = $("<a href='"+link+"' class='"+link+"'>").text(link);
new_row.find("."+link).append(a)
}
}
return new_row
};
$("#addRowAndEdit").click(function(e){
e.preventDefault();
var row = getNewRow();
$(".my_table").append(row);
new_record = $('.my_table tbody>tr:last');
new_record.find('a.edit').click(); //this is the line that is not working.
....
}
UPDATE:更新:
If I do new_record.find('a.edit').on('click', alert('ok'));
如果我做
new_record.find('a.edit').on('click', alert('ok'));
, the alert funcion works, but the other one is not called. ,警报功能有效,但未调用另一个。
Any ideas?有任何想法吗? Thanks
谢谢
Use event delegation so your new added rows are handled by a handler attached to the table, instead of attaching it to each individual a
element, for that, replace the following line in inplacerowedit.js
:使用事件委托,以便新添加的行由附加到表的处理程序处理,而不是将其附加到每个单独
a
元素,为此,替换inplacerowedit.js
的以下行:
$(this).find(ops.editbuttonselector).on('click', function(e) {
for this one:对于这个:
$(this).on('click', ops.editbuttonselector, function(e) {
EDITED已编辑
If you are using latest jquery versions of 1.6+ then you should try this:如果您使用的是 1.6+ 的最新 jquery 版本,那么您应该试试这个:
new_record.find('a.edit').live('click',function(){
// All your desired stuff here
});
try and see if this works for you.试试看这是否适合你。
Just because you are dynamically creating the rows
, for this kind of situation .live()
event handler is useful. Just because you are dynamically creating the rows
,对于这种情况.live()
事件处理程序很有用。
you can find more information here: http://api.jquery.com/live/你可以在这里找到更多信息: http : //api.jquery.com/live/
I had to make 2 changes: One as suggested by nelson (for some reason, the first time I tried his suggestion, it had broken the existing links) and another one like this: new_record.find('a.edit')[0].click()我必须进行 2 个更改:一个是 nelson 建议的(出于某种原因,我第一次尝试他的建议时,它破坏了现有链接)和另一个像这样:new_record.find('a.edit')[0 ]。点击()
I found the answer at https://stackoverflow.com/a/12110482/210481我在https://stackoverflow.com/a/12110482/210481找到了答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.