简体   繁体   English

XUL 获取被选中的 MENUITEM 的值

[英]XUL get the value of MENUITEM selected

I am creating a dropdown in XUL like this:我正在像这样在 XUL 中创建一个下拉列表:

<button id="bid" oncommand="alert(event.target.nodeName)" >
    <menupopup>
              <menuitem label="one" value="one" />  
              <menuitem label="two" value="two" />
              <menuitem label="three" value="three" />
    </menupopup>
</button>

I want to alert the value of nodeitem which is being clicked.我想提醒正在单击的 nodeitem 的值。 Using event.target.nodeName giving nodeName as menuitem but using nodeValue is retuning undefined as it starts to take value of button node.使用event.target.nodeName nodeName作为menuitem但使用nodeValue正在重新调整 undefined 因为它开始采用button节点的值。 How can I get the value of the menuitem clicked.如何获得单击的菜单项的值。 I also tried $(this).val() but even that gave undefined.我也试过$(this).val()但即使这样也没有定义。 Using $(this).attr('value') also didn't help.使用$(this).attr('value')也没有帮助。 I can put oncommand for menuitem but that doesn't seem to be actual solution.我可以为 menuitem 设置 oncommand ,但这似乎不是实际的解决方案。 Is that the only way or some method exist to get the value?这是获得价值的唯一方法或某种方法吗?

The XUL code you have in the question is non-functional.您在问题中的 XUL 代码不起作用。 Either you should be using a <menulist> instead of a <button> , or your <button> needs the property type="menu" or type="menu-button" .您应该使用<menulist>而不是<button> ,或者您的<button>需要属性type="menu"type="menu-button" If you are going to use a <button> without the type="menu" property, you would actually have to open a popup in the command event handler for the <button> and then select from there.如果您打算使用没有type="menu"属性的<button> ,您实际上必须在<button>的命令事件处理程序中打开一个弹出窗口,然后从那里选择。 That does not appear to be what you are wanting.这似乎不是你想要的。 It is, however, quite doable.然而,这是完全可行的。 I use a construction like that in one of my add-ons, except I have it open when one of several<label> items is clicked.我在我的一个附加组件中使用了类似的结构,除了在单击多个<label>项目之一时我将其打开。 This allows re-use of a single <menupopup> for multiple different items in the window.这允许将单个<menupopup>用于窗口中的多个不同项目。 In that case, the <menupopup> is a rather large amount XUL code that I did not want to repeat and maintain multiple duplicates in the XUL for the window.在那种情况下, <menupopup>是一个相当大的 XUL 代码,我不想在窗口的 XUL 中重复和维护多个重复项。

The value of the <menuitem> selected:选择的<menuitem>value

Your actual question is how to get the value of the <menuitem> selected by the user.您的实际问题是如何获取用户选择的<menuitem>value All of the code below is tested and functional.下面的所有代码都经过测试并且可以正常运行。 As you can see from the code you can get the value of the <menuitem> you select (at the time of selection) from: event.target.value .正如您从代码中看到的,您可以从以下位置获取您选择(在选择时)的<menuitem>valueevent.target.value

Using a <menulist> element:使用<menulist>元素:

The most common element to use would be a <menulist> .最常用的元素是<menulist> This would normally be used when you are having the user select from multiple options a choice that is going to be remembered and used later, or used to adjust what is presented in the user interface.这通常用于让用户从多个选项中选择一个将被记住并稍后使用的选项,或者用于调整用户界面中显示的内容。 It would generally not be used to select from multiple immediate actions (which are acted upon and the choice not remembered).它通常不会用于从多个即时操作(已执行但未记住选择)中进行选择。 A <menulist> is what is used in the examples for <menuitem> and <menupopup> on MDN. <menulist>是 MDN 上<menuitem><menupopup>示例中使用的内容。

<menulist id="bid2" oncommand="alert(event.target.value)" >
    <menupopup>
              <menuitem label="one" value="one" />  
              <menuitem label="two" value="two" />
              <menuitem label="three" value="three" />
    </menupopup>
</menulist>

The above code will give you what looks like a button with a selection of <menuitem> entries when you click on it.当你点击它时,上面的代码会给你一个看起来像一个带有<menuitem>条目选择的按钮。 It will alert with the value of the <menuitem> you have selected.它将用您选择的<menuitem>的值<menuitem> alert

This will produce an item that looks like:这将产生一个看起来像这样的项目:
<menuitem> 关闭
Which you can click on to open a drop-down list:您可以单击以打开下拉列表:
<menuitem> 打开
If you select an item:如果您选择一个项目:
<menuitem> 打开,两个选中
You will get an alert:您将收到警报:
警报,两个
And the object will then show your selection:然后对象将显示您的选择:
<menuitem> 关闭,两个被选中

Using a <button> element:使用<button>元素:

It is also possible to use a <button> element with the property type="menu" or type="menu-button" specified.也可以使用指定了属性type="menu"type="menu-button"<button>元素。 However, this provides no visual feedback to the user as to which option is currently selected.然而,这不会向用户提供关于当前选择了哪个选项的视觉反馈。 [Note: Your JavaScript could manually change the <button> label property to provide this feedback.] You could use this type of element if it is button that produces an immediate action rather than a selection that is remembered. [注意:您的 JavaScript 可以手动更改<button> label属性以提供此反馈。] 如果按钮产生即时操作而不是记住的选择,您可以使用这种类型的元素。

The code:编码:

<button type="menu" id="bid2" label="A Button" oncommand="alert(event.target.value)">
    <menupopup>
              <menuitem label="one" value="one" /> 
              <menuitem label="two" value="two" />
              <menuitem label="three" value="three" />
    </menupopup>
</button>

This will produce an item that looks like:这将产生一个看起来像这样的项目:
<按钮> 关闭
Which you can click on to open a drop-down list:您可以单击以打开下拉列表:
<按钮> 打开
If you select an item:如果您选择一个项目:
<button> 打开,三个选中
You will get an alert:您将收到警报:
警报三
And the object will then NOT show your selection:然后该对象将不会显示您的选择:
<按钮> 关闭

If you want to set the label of the <button> to reflect the selection made by the user, you could use:如果要设置<button>的标签以反映用户所做的选择,可以使用:

<button type="menu" id="bid2" label="A Button" oncommand="event.target.parentElement.parentElement.label=event.target.value;alert(event.target.value)">
    <menupopup>
              <menuitem label="one" value="one" /> 
              <menuitem label="two" value="two" />
              <menuitem label="three" value="three" />
    </menupopup>
</button>

When three is selected, that will result in a button that looks like:选择three ,将导致按钮看起来像:
在此处输入图片说明

Using a <toolbarbutton> :使用<toolbarbutton>

You could, alternately, use a <toolbarbutton> .或者,您可以使用<toolbarbutton>

When not hovered, doing so would look like:当不悬停时,这样做看起来像:
<toolbarbutton> 关闭,未悬停
When hovered:悬停时:
<toolbarbutton> 关闭,悬停
When open for selection:打开供选择时:
<工具栏按钮> 打开

Choices in UI design: UI设计中的选择:

There are many choices that you have to make when designing your user interface.在设计用户界面时,您必须做出许多选择。 There are many different ways to get to the same effective result, with somewhat different look and feel.有许多不同的方法可以获得相同的有效结果,但外观和感觉略有不同。 You really should be trying these types of options on your own.您真的应该自己尝试这些类型的选项。 You may find the XULRunner program XUL Explorer to be of use when prototyping XUL.在对XUL进行原型设计时,您可能会发现 XULRunner 程序XUL Explorer很有用。

Selecting UI elements and a look and feel is, in my opinion, beyond the scope of questions on stackoverflow.在我看来,选择 UI 元素和外观超出了 stackoverflow 的问题范围。 While you probably won't get specific XUL help, you can ask UI design questions at: the User Experience stack exchange .虽然您可能不会获得特定的 XUL 帮助,但您可以在以下位置提出 UI 设计问题:用户体验堆栈交换

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

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