简体   繁体   English

单击选定元素内部的生成字段时,如何防止jQuery .click函数运行?

[英]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.

相关问题 单击由函数生成的jQuery对话框中的元素 - Clicking on element from jQuery dialog generated by function 单击提交时如何防止表单内的onchange运行 - how to prevent an onchange inside a form from running when clicking submit 当已选择数组中的DOM元素时,如何阻止javascript函数运行? - How do I prevent a javascript function from running when a DOM element from an array is already selected? 如何防止点击子元素触发点击它所在的 label? - How to prevent clicking on a child element triggering the click on the label that it is inside? 如何防止单击元素(jQuery)后继续发生任何事件? - How to prevent any event from continuing after clicking on an element (jQuery)? 使用jQuery单击动态生成的链接时如何调用函数? - How to call a function when clicking a dynamically generated link, using jQuery? 当元素由脚本动态生成并通过 class 选择器访问时,如何在 Jquery 中的单击事件上调用 function? - How to call function on click event in Jquery when the element is generated dynamically by script and access through class selector? Jquery - 单击jquery函数生成的元素 - Jquery - click on element generated by jquery function 如何防止单击功能在双击时运行? - How to prevent click function from running on double click? 如何在引导程序中单击元素内部时禁用切换功能 - How to disable toggle function when clicking inside of the element in the bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM