选择Js会自动触发onload

时间:2015-07-03 06:11:11

标签: jquery selectize.js

有没有办法在加载时触发select。我正在使用selectize插件。这个插件没有在加载时触发。一旦手动选择,它就可以工作。

https://github.com/brianreavis/selectize.js

这里我需要将选项ID与IP检测国家/地区值进行比较。例如,如果两个AU都触发该选项。

HTML

<select name="select" id="select" >
<option id="EN" value="global.html">Global</option>
<option id="AU" value="australia.html">Australia</option>
<option id="ID" value="Indonesia.html">Indonesia</option>
</select>

在浏览器中,它呈现如下格式:

<select name="select" id="select" class="custom-select elem-width selectized" placeholder="Select Country" data-validation="required" tabindex="-1" style="display: none;">
    <option value="others.html" selected="selected"></option>
</select>

<div class="selectize-control custom-select elem-width single">
    <div class="selectize-input items full has-options has-items">
        <div data-value="others.html" class="item">Others</div>
        <input type="text" autocomplete="off" tabindex="" style="width: 106px; opacity: 0; display: inline-block;">
    </div>
    <div class="selectize-dropdown custom-select elem-width single" style="display: none; visibility: visible; width: 235px; top: 40px; left: 0px;">
        <div class="selectize-dropdown-content">
            <div data-value="others.html" data-selectable="" class="option selected">Others</div>
            <div data-value="US.html" data-selectable="" class="option">United States (US)</div>
            <div data-value="kr.html" data-selectable="" class="option">Korea</div>
        </div>
    </div>
</div>

JS

function getCode () {
var now = new Date().getTime();
var randomNumber = Math.floor((Math.random() * 100) + 1);
var uniqueNumber = now + 'a' + randomNumber;
$.getScript("getCountryCode.js?" + uniqueNumber, function(data, textStatus, jqxhr) {
    if (country){
       var code = country;
       console.log("countryCode:" + countryCode);
       $('.selectize-dropdown-content div').trigger('change');
     }
});
}

1 个答案:

答案 0 :(得分:0)

我假设您返回json来电的ajax数据格式如下:

data={"country":"australia.html","countryCode":"AU"}

现在,您将使用data检查if(data.country)对于国家/地区名称是否有效。然后你可以使用下面的代码来改变价值!!无需trigger更改!您只需使用setValue的{​​{1}}选项设置值,如下所示:

selectize

<强> DEMO