简体   繁体   English

使用Javascript获取选择选项值

[英]Getting Select Option Value with Javascript

I have a dropdown menu and when "Internal" is selected I want to hide certain text (The id="hide-this" part). 我有一个下拉菜单,当选择“内部”时,我想隐藏某些文本(id =“ hide-this”部分)。 Why isn't my code working? 为什么我的代码不起作用? Also, this needs to be executed with Javascript! 另外,这需要使用Javascript执行!

 HTML
       <tr>
            <td class="name">Affiliate Type: </td>
            <td class="value">
                <div style="float:left;">
                    <select id="affiliate_type" size="0" name="affiliate_type">
                      <option value="internal">Internal</option>
                      <option value="external">External</option>
                      <option value="network" selected="selected">Network</option>
                    </select>                   
                </div>

                <div id="hide-this" style="float:left;">
                    <label for="web_access_1"><input type="checkbox" id="web_access_1" name="web_access[]" value="1">Restrict access to web</label>                        
                    <label for="app_access_1"><input type="checkbox" id="app_access_1" name="app_access[]" value="1" checked="checked">Restrict access to app</label>  
                </div>
            </td>
        </tr>

Javascript 使用Javascript

 var sel = document.getElementById('affiliate_type');
 if (sel.options[sel.selectedIndex].value == 'internal') {
     document.getElementById('hide-this').style.display = 'none';
}

You have to call your code! 您必须调用您的代码! Attach an onchange handler: 附加一个onchange处理程序:

document.getElementById('affiliate_type').onchange = function() {
    if (this.options[this.selectedIndex].value == 'internal') {
        document.getElementById('hide-this').style.display = 'none';
    }
}

Fiddle: http://jsfiddle.net/xdonxwp1/ 小提琴: http : //jsfiddle.net/xdonxwp1/

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

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