[英]How to prevent jQuery .click function from running when clicking in generated field inside selected element?
So I'm trying to do some inline text editing with jQuery and ajax. 因此,我尝试使用jQuery和Ajax进行一些内联文本编辑。 The code does the job but I ran into an issue regarding input field generation.
代码可以完成工作,但是我遇到了有关输入字段生成的问题。
Here's the part of code generating input field: 这是代码生成输入字段的一部分:
$('.edit_area').click(function() {
old_value = $(this).text();
$('.ajax').html(old_value);
$('.ajax').removeClass('ajax');
$(this).addClass('ajax');
$(this).html('<input id="editbox" type="text" size="'+$(this).text().length+'" value="'+old_value+'">');
$('#editbox').focus();
});
It basically generates input field in the place of original text. 它基本上会在原始文本的位置生成输入字段。 Variable old_value stores original text and is used for default input value and to restore original text if user cancels editing.
变量old_value存储原始文本,并用作默认输入值,并在用户取消编辑时恢复原始文本。 If user clicks on different element with class .edit_area the previous returns to original state and user can edit that other one.
如果用户单击类为.edit_area的其他元素,则先前的元素将返回原始状态,并且用户可以编辑其他元素。 It's just what i need for my project.
这正是我需要的项目。
Here is the problem: the function runs even if I click inside the generated input field. 这是问题所在:即使我在生成的输入字段中单击,该函数也会运行。 Is there any way to fix this?
有没有什么办法解决这一问题?
Try to use jquery find to check whether the textbox exists or not. 尝试使用jquery find检查文本框是否存在。
var isfound=$( ".edit_area" ).find( "#editbox" );
if(isfound.length!=0) return;
http://jsfiddle.net/bochzchan/eURtZ/ http://jsfiddle.net/bochzchan/eURtZ/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.