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

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论