用于从组合框中获取选定值的 JavaScript 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10007774/
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-08-23 23:27:33  来源:igfitidea点击:

JavaScript code for getting the selected value from a combo box

javascriptcombobox

提问by user181891

Can any one give me a sample code that gets the selected value from an existing combo box?

谁能给我一个示例代码,从现有的组合框中获取选定的值?

I have this code but its not doing anything:

我有这个代码,但它没有做任何事情:

function check () 
{
    var e = document.getElementById("ticket_category_clone");
    var str = e.options[e.selectedIndex].text;

    alert(str);

    if (str==="Hardware")
    {
        SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
    }       
}

SPICEWORKS.app.helpdesk.ready(check);?

heres a imgof the code

这是代码的img

and the code

和代码

<select id="ticket_category_clone" name="ticket[category]" hdpp="ticket_category">
<option value=""></option><option value="Hardware">Hardware</option>
<option value="Software">Software</option>
<option value="Rede" selected="selected">Rede</option>
<option value="Pedidos">Pedidos</option>
<option value="Forma??o/Dúvida">Forma??o/Dúvida</option>
<option value="Outro">Outro</option><option value="#edit_categories#">Edit Categories...</option></select>

what i want its find a way to get the selected value fo that combobox

我希望它找到一种方法来获取该组合框的选定值

回答by Willem D'Haeseleer

There is an unnecessary hashtag; change the code to this:

有一个不必要的标签;将代码更改为:

var e = document.getElementById("ticket_category_clone").value;

回答by ThdK

I use this

我用这个

var e = document.getElementById('ticket_category_clone').value;

Notice that you don't need the '#' character in javascript.

请注意,您不需要 javascript 中的“#”字符。

    function check () {

    var str = document.getElementById('ticket_category_clone').value;

      if (str==="Hardware")
      {
        SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
      }

    }

SPICEWORKS.app.helpdesk.ready(check);?

回答by Sjuul Janssen

It probably is the # sign like tho others have mentioned because this appears to work just fine.

它可能是其他人提到的 # 符号,因为这似乎工作得很好。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

</head>
<body>

    <select id="#ticket_category_clone">
  <option value="hw">Hardware</option>
  <option>fsdf</option>
  <option>sfsd</option>
  <option>sdfs</option>
</select>
<script type="text/javascript">
    (function check() {
        var e = document.getElementById("#ticket_category_clone");
        var str = e.options[e.selectedIndex].text;

        alert(str);
        if (str === "Hardware") { 
            alert('Hi'); 
        }


    })();
</script>
</body>