javascript 道场:道场 onblur 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4662646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:28:03  来源:igfitidea点击:

Dojo: dojo onblur events

javascriptdojodom-eventsonblurdijit.form

提问by Jane WIlkie

I have a form setup with dojo 1.5. I am using a dijit.form.ComboBox and a dijit.form.TextBox

我有一个带有 dojo 1.5 的表单设置。我正在使用 dijit.form.ComboBox 和 dijit.form.TextBox

The Combobox has values like "car","bike","motorcycle" and the textbox is meant to be an adjective to the Combobox. So it doesn't matter what is in the Combobox but if the ComboBox does have a value then something MUST be filled in the TextBox. Optionally, if nothing is in the ComboBox, then nothing can be in the TextBox and that is just fine. In fact if something isn't in the Combobox then nothing MUST be in the text box.

组合框具有诸如“汽车”、“自行车”、“摩托车”之类的值,文本框是组合框的形容词。因此,组合框中的内容并不重要,但是如果组合框确实具有值,则必须在文本框中填充某些内容。或者,如果 ComboBox 中没有任何内容,则 TextBox 中不能有任何内容,这很好。事实上,如果组合框中没有内容,则文本框中必须没有内容。

In regular coding I would just use an onBlur event on the text box to go to a function that checks to see if the ComboBox has a value. I see in dojo that this doesn't work... Code example is below...

在常规编码中,我只会使用文本框上的 onBlur 事件转到检查 ComboBox 是否具有值的函数。我在 dojo 中看到这不起作用......代码示例如下......

Vehicle:
    <input dojoType="dijit.form.ComboBox"
      store="xvarStore"
      value=""
      searchAttr="name"
      name="vehicle_1"
      id="vehicle_1"
    />
 Descriptor:
<input type="text"
                dojoType="dijit.form.TextBox"
                value=""
                class=lighttext
                style="width:350px;height:19px"
                id="filter_value_1"
                name="filter_value_1"
                />

My initial attempt was to add an onBlur within the Descriptor's <input> tag but discovered that that doesn't work.

我最初的尝试是在 Descriptor 的 <input> 标记中添加一个 onBlur,但发现这不起作用。

How does Dojo handle this? Is it via a dojo.connect parameter? Even though in the example above the combobox has an id of "vehicle_1" and the text box has an id of "filter_value_1", there can be numerous comboboxes and textboxes numbering sequentially upward. (vehicle_2, vehicle_3, etc)

Dojo 如何处理这个问题?是通过 dojo.connect 参数吗?即使在上面的示例中,组合框的 id 为“vehicle_1”,文本框的 id 为“filter_value_1”,但可以有许多组合框和文本框按顺序向上编号。(车辆_2、车辆_3 等)

Any advice or links to resources would be greatly appreciated.

任何建议或资源链接将不胜感激。

回答by GreenWebDev

To add the onBlur event you should use dojo.connect():

要添加 onBlur 事件,您应该使用 dojo.connect():

dojo.connect(dojo.byId("vehicle_1"), "onBlur", function() { /* do something */ });

If you have multiple inputs that you need to connect this to, consider adding a custom class for those that need to blur and using dojo.query to connect to all of them:

如果您有多个输入需要连接,请考虑为需要模糊的输入添加自定义类并使用 dojo.query 连接到所有输入:

Vehicle:
    <input dojoType="dijit.form.ComboBox"
      store="xvarStore"
      class="blurEvent" 
      value=""
      searchAttr="name"
      name="vehicle_1"
      id="vehicle_1"
    />

dojo.query(".blurEvent").forEach(function(node, index, arr) {
      dojo.connect(node, "onBlur", function() { /* do something */ });
  });

In the function that is passed to dojo.connect you could add in some code to strip out the number on the end and use it to reference each filter_value_* input for validation.

在传递给 dojo.connect 的函数中,您可以添加一些代码以去除末尾的数字,并使用它来引用每个 filter_value_* 输入以进行验证。

dojo.connect()

dojo.connect()

Combobox documention

组合框文档

回答by Ken Franqueiro

onBlurseems to work just fine for me, even in the HTML-declared widgets. Here's a very rudimentary example:

onBlur似乎对我来说工作得很好,即使在 HTML 声明的小部件中也是如此。这是一个非常基本的例子:

http://jsfiddle.net/kfranqueiro/BWT4U/

http://jsfiddle.net/kfranqueiro/BWT4U/

(Have firebug/webkit inspector/IE8 dev tools open to see console.log messages.)

(打开 firebug/webkit 检查器/IE8 开发工具以查看 console.log 消息。)

However, for a more ideal solution to this, you might also be interested in some other widgets...

但是,对于更理想的解决方案,您可能还对其他一些小部件感兴趣......

Hopefully this can get you started.

希望这可以让你开始。