javascript - How to prevent event OnChange on dojo Select - Stack Overflow

I am working on localization and use cookie to store language. I have a Select:var select = new dijit.f

I am working on localization and use cookie to store language. I have a Select:

var select = new dijit.form.Select({
    name: "languageSelect",
    onChange: function (value) {
        changeLanguage(value);
    }
}, "languageSelect");

in onChange event, will call method changeLanguage:

 var changeLanguage = function (language) {
    $.ajax({
        type: "PUT",
        url: "/api/accounts/changelanguage?language=" + language,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            window.location.reload(false);
        }
    });
};

This method with do ajax call to server and then re-load page.

The problem is: on the page load, I need to read cookie language which user chose before and set it to Select:

select.set("value", readCookie("language"));

But, this set causes the event OnChange fire, then make page re-load which I don't want at the beginning. I tried to use one flag like below but it does not work out:

var select = new dijit.form.Select({
    name: "languageSelect",
    style: { width: '200px' },
    onChange: function (value) {
        if (isLoading == false)
            changeLanguage(value);
    }
}, "languageSelect");

var isLoading = true;
select.set("value", readCookie("language"));
isLoading = false;

Is there anyway to sort it out, I was thinking to prevent event OnChange fire on set method, but have not found out how to do it.

I am working on localization and use cookie to store language. I have a Select:

var select = new dijit.form.Select({
    name: "languageSelect",
    onChange: function (value) {
        changeLanguage(value);
    }
}, "languageSelect");

in onChange event, will call method changeLanguage:

 var changeLanguage = function (language) {
    $.ajax({
        type: "PUT",
        url: "/api/accounts/changelanguage?language=" + language,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            window.location.reload(false);
        }
    });
};

This method with do ajax call to server and then re-load page.

The problem is: on the page load, I need to read cookie language which user chose before and set it to Select:

select.set("value", readCookie("language"));

But, this set causes the event OnChange fire, then make page re-load which I don't want at the beginning. I tried to use one flag like below but it does not work out:

var select = new dijit.form.Select({
    name: "languageSelect",
    style: { width: '200px' },
    onChange: function (value) {
        if (isLoading == false)
            changeLanguage(value);
    }
}, "languageSelect");

var isLoading = true;
select.set("value", readCookie("language"));
isLoading = false;

Is there anyway to sort it out, I was thinking to prevent event OnChange fire on set method, but have not found out how to do it.

Share Improve this question asked Feb 28, 2013 at 4:33 cuonglecuongle 75.3k30 gold badges154 silver badges212 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

try adding false, like:

select.set("value", readCookie("language"));

to

select.set("value", readCookie("language"), false);

false flag at the end of the set() method that will prevent the onChange event.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742353912a4428058.html

相关推荐

  • javascript - How to prevent event OnChange on dojo Select - Stack Overflow

    I am working on localization and use cookie to store language. I have a Select:var select = new dijit.f

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信