简体   繁体   English

Javascript事件触发器

[英]Javascript event triggers

I'm trying to create an Ajax enabled webform, and have created a simple page. 我正在尝试创建启用了Ajax的网络表单,并创建了一个简单页面。 On the page, I have a div that contains the value I wish to edit. 在页面上,我有一个div,其中包含我要编辑的值。

I've got working javascript to add a text input box around the contents of the div, complete with a Save button to post back to the web server, but unlike the example I'm following, I don't want a cancel button, instead I want the cancel operation to be triggered by the input box losing focus. 我可以使用javascript在div内容周围添加一个文本输入框,并带有一个保存按钮以发布回Web服务器,但是与下面的示例不同,我不需要取消按钮,相反,我希望取消操作由失去焦点的输入框触发。

I've added a onblur event to the text input box to cancel the edit operation and return the form back to it's original unedited state (and not save the change). 我在文本输入框中添加了一个onblur事件,以取消编辑操作,并将表单恢复为原始未编辑状态(并且不保存更改)。

The problem I'm having, is that when I click the 'Save' button, the onblur event of the text input box is also triggered. 我遇到的问题是,当我单击“保存”按钮时,也会触发文本输入框的onblur事件。 How can I stop this happening? 我该如何阻止这种情况发生?

The edit function (called when a user clicks on the text to be edited) is as follows: 编辑功能(当用户单击要编辑的文本时调用)如下:

function edit(obj)
{
  Element.hide(obj);
  var textarea = '<div id="'+obj.id+'_editor"><div class="fieldvalueedit"><input type="text" id="'+obj.id+'_edit" name="'+obj.id+'" rows="1" size="64" value="'+obj.innerHTML+'"></div>';
  var button = '<div class="fieldvaluebuttons"><input id="'+obj.id+'_save" type="button" value="Save"/></div></div>';

  new Insertion.After(obj, textarea+button);
  document.getElementById(obj.id+'_edit').focus();
  Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj)}, false);
  Event.observe(obj.id+'_edit', 'blur', function(){cleanUp(obj)}, false);
}

Instead of using an onblur event on input you can register an onclick event on the document that resets the textareas state, then cancel the propagation of this event when the save button is clicked (you will want to apply this to the text area as well). 您可以在文档上注册一个onclick事件来重置textareas状态,而不是在输入中使用onblur事件,然后在单击保存按钮时取消此事件的传播(您也希望将此事件应用于文本区域) 。

Event.observe(obj.id+'_save', 'click', function( e ){
    e.stopPropagation();
    saveChanges(obj);
}, false);
Event.observe(document, 'click', function(){cleanUp(obj)}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM