简体   繁体   English

从下拉项中传递一个值

[英]Passing a value from a dropdown item

I have a dropdown menu.我有一个下拉菜单。 Each entry has an ID and a value.每个条目都有一个 ID 和一个值。 Clicking on an item on the list calls the click-handler and passes the div.单击列表中的项目会调用单击处理程序并传递 div。 The click handler can access the id, but it doesn't work for the value.点击处理程序可以访问 id,但它对值不起作用。

When I log the parameter that is passed to the event handler, it shows this string:当我记录传递给事件处理程序的参数时,它会显示以下字符串:

<div class="dropdown-item" id="chooseLeagueLevelDD" value="0" onmouseup="mUp(this)">1. Bundesliga</div>

I am able to determine the event using the event handler.我能够使用事件处理程序确定事件。 If I log obj.id, the correct string is displayed.如果我记录 obj.id,则会显示正确的字符串。 If I try to log obj.value , it's undefined .如果我尝试记录obj.value ,它是undefined

function mUp(obj) {
        if (obj.id == "pauseGame"){
                pauseGame();
        }
}

What am I missing?我错过了什么?

div doesn't have a native value property or attribute, adding a value attribute to the html is basically adding a custom attribute so you will need to treat it as such. div 没有本机值属性或属性,将值属性添加到 html 基本上是添加自定义属性,因此您需要这样对待它。 If you use the getAttribute() method on the event parameter you should be able to get your data.如果您在事件参数上使用 getAttribute() 方法,您应该能够获取数据。

function mUp(obj) {
        if (obj.getAttribute('value') == "pauseGame"){
            pauseGame();
       }
}

Try:尝试:

function mUp(obj) {
        console.log(obj.attributes.value.nodeValue);
}

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

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