开发者

jQuery onselected for a option, how to?

开发者 https://www.devze.com 2022-12-21 05:47 出处:网络
<select> <option value=\"0\" id=\"n\">n</option> <option value=\"1\" id=\"m\">m</option>
<select>
            <option value="0" id="n">n</option>
            <option value="1" id="m">m</option>
</select>

In jQuery, how do I say:

When id "n"开发者_StackOverflow中文版 is selected...do this . (not onclick) I want onSelected.


You could use the change eventhandler

http://api.jquery.com/change/


Instead of writing onclick for each option tag, I recomment use onchange event of select. Another advantage of writing this will be you don't need to care event bubbling or event capturing and you have one method declared on select tag.


No need to use id's for options inside a select tag.

$(function(){
    $("#sel1").change(function(){
        switch($(this).val())
        {
            case "0": // do your code
                      break;
        }
    });
});
<select id="sel1">
            <option value="0" id="n">n</option>
            <option value="1" id="m">m</option>
</select>
0

精彩评论

暂无评论...
验证码 换一张
取 消