最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Run javascript function if option selected - Stack Overflow

programmeradmin1浏览0评论

If I have a select like this

<select id="selectid" name="selectname" onchange="jsfunc1()">
    <option value="val1" id="valid1"> Val1 </option>
    <option value="val2" id="valid2"> Val2 </option>
    <option value="val3" id="valid3"> Val3 </option>
</select>

I now have a javascript function func2, say, that need to do something if option val1 is selected. How do I do that?

For example,

function func2(){
    ....
    if(document.getElementById('valid2').selected==True){
        //Do something 
    }
}

I'm not getting the exact syntax right and that's where I need your help.

If I have a select like this

<select id="selectid" name="selectname" onchange="jsfunc1()">
    <option value="val1" id="valid1"> Val1 </option>
    <option value="val2" id="valid2"> Val2 </option>
    <option value="val3" id="valid3"> Val3 </option>
</select>

I now have a javascript function func2, say, that need to do something if option val1 is selected. How do I do that?

For example,

function func2(){
    ....
    if(document.getElementById('valid2').selected==True){
        //Do something 
    }
}

I'm not getting the exact syntax right and that's where I need your help.

Share Improve this question edited Nov 29, 2017 at 16:30 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Aug 18, 2013 at 21:14 crazyim5crazyim5 7142 gold badges9 silver badges26 bronze badges 2
  • 2 Careful, case sensitivity, True != true. – elclanrs Commented Aug 18, 2013 at 21:15
  • if(document.getElementById('selectid').value=='val1') – nnnnnn Commented Aug 18, 2013 at 21:16
Add a comment  | 

1 Answer 1

Reset to default 26

I guess that this will work for you.

if(document.getElementById('selectid').value == "val1") {
     //Do something
}
发布评论

评论列表(0)

  1. 暂无评论