0

I have the following code:

$("select#sel_mode").change(function(){
    if ($(this).val() == "N" || $(this).val() == "Y"){
        $("table#options").hide();
    } else {
        $("table#options").show();
        if ($(this).val() == "C"){
            $("tr#ip").hide();
        } else {
            $("tr#ip").show();
        }
    }
});

It works perfectly when I manually do changes in selectbox.

Using ajax I get values from server and according to the value I need to put selectbox in some position like:

$('#sel_mode').val(json_answer.mode);

But it will not show/hide necessary elements and I need to add similar code again:

    if (json_answer.mode == "N" || json_answer.mode == "Y"){
        $("table#options").hide();
    } else {
        $("table#options").show();
        if (json_answer.mode == "C"){
            $("tr#ip").hide();
        } else {
            $("tr#ip").show();
        }
    }

Is it possible somehow forcibly call defined method .change for select box? So i need to avoid duplicate code. Give me advice please. Thanks.

4

1 回答 1

2

您可以调用不带参数的 jQuery 更改函数来强制该函数运行。

$("select#sel_mode").change();
于 2013-04-19T09:24:14.413 回答