开发者

How to set onchange based on count using jquery

I need to set onchange function in dynamically based on count , for example if Count = 2,Need to apply function test() in select

<select onChange="test()"> Is it possible?

Thanks, Di开发者_开发问答nesh Kumar M


Rather than encoding it in your select, try something like this:

$(document).ready(function() {
    $("select").change(testForChange); // assuming this is the only select on the page
});

function testForChange() {
    // assume count is the value of the selected option
    if($("select option:selected").val() === 2) { 
         test();
    }
}

function test() {
    //...
}

With the code written this way you remove the onchange handler from your HTML, a plus for the goal of having unobtrusive Javascript.


If count is the number of options in the select list you can achieve this like so:

$("select").change(function(){
    var count = $(this).find("option").length;

    switch(count)
    {
        case 1: call1(); break;
        case 2: call2(); break;
        case 3: call3(); break;
    }
});


try this:

$('select').change(function(){
   //do something on select change
});

jQuery Ref

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜