在Select - HTML中获取选项的值

时间:2015-03-19 22:29:05

标签: javascript html

我有以下代码:

<select id="Negrillon">
    <option value="" disabled selected>Couleurs</option>
    <option value="#588c75" data-class="color-588c75">#588c75</option>
    <option value="#b0c47f" data-class="color-b0c47f">#b0c47f</option>
    <option value="#f3e395" data-class="color-f3e395">#f3e395</option>
    <option value="#f3ae73" data-class="color-f3ae73">#f3ae73</option>
    <option value="#da645a" data-class="color-da645a">#da645a</option>
</select>

我会在JavaScript中获得选项值。我试过这个,但它不起作用。如果你能帮助我,那真的很酷。

var e = document.getElementById("Negrillon");
var strUser = e.options[e.selectedIndex].text;

1 个答案:

答案 0 :(得分:1)

而不是

var strUser = e.options[e.selectedIndex].text;

你应该用这个:

var strUser = e.options[e.selectedIndex].value;