簡體   English   中英

用於獲取select選項值的javascript代碼

[英]javascript code for getting the select option value

我正在使用代碼來檢測選擇框的選定選項的值,我正在使用以下代碼和firefox,它工作正常,但是在chrome和IE上,它拋出錯誤。 由於項目限制,我無法在此處使用jquery:

var regID= document.getElementById("tdd");
            if (regID != null || regID != 'undefined' || regID != undefined) {
                var passvalue = regID.options[regID.selectedIndex].value;
}

Error by Chrome: Uncaught TypeError: Cannot read property 'value' of undefined 

Firefox沒有引發任何錯誤

只需查看<select>value

jsfiddle: http : //jsfiddle.net/7kr6x7uh/更新了小提琴,其中添加了檢查以查看您的<select>是否為空: http : //jsfiddle.net/7kr6x7uh/1/

的HTML:

<select id="tdd">
    <option val="1">Option 1</option>
    <option val="2">Option 2</option>
    <option val="3" selected="selected">Option 3</option>
</select>

javascript:

var select = document.getElementById('tdd');
if (select){
    alert(tdd.value); //  in your case, do 'var passvalue = tdd.value;'
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM