简体   繁体   中英

XUL get the value of MENUITEM selected

I am creating a dropdown in XUL like this:

<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. Using event.target.nodeName giving nodeName as menuitem but using nodeValue is retuning undefined as it starts to take value of button node. How can I get the value of the menuitem clicked. I also tried $(this).val() but even that gave undefined. Using $(this).attr('value') also didn't help. I can put oncommand for menuitem but that doesn't seem to be actual solution. Is that the only way or some method exist to get the value?

The XUL code you have in the question is non-functional. Either you should be using a <menulist> instead of a <button> , or your <button> needs the property type="menu" or 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. 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. This allows re-use of a single <menupopup> for multiple different items in the window. 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.

The value of the <menuitem> selected:

Your actual question is how to get the value of the <menuitem> selected by the user. 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 .

Using a <menulist> element:

The most common element to use would be a <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 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. It will alert with the value of the <menuitem> you have selected.

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:

It is also possible to use a <button> element with the property type="menu" or type="menu-button" specified. 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.

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 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:
在此处输入图片说明

Using a <toolbarbutton> :

You could, alternately, use a <toolbarbutton> .

When not hovered, doing so would look like:
<toolbarbutton> 关闭,未悬停
When hovered:
<toolbarbutton> 关闭,悬停
When open for selection:
<工具栏按钮> 打开

Choices in UI design:

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.

Selecting UI elements and a look and feel is, in my opinion, beyond the scope of questions on stackoverflow. While you probably won't get specific XUL help, you can ask UI design questions at: the User Experience stack exchange .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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