在Laravel(刀片文件)中为Jquery select设置select



我知道我可以用旧的put文件为select标记设置selected属性:

<select name="country" id="state" onChange="loadlist(this.value);" >
<option value="usa" {{ (old('state') == 'usa') ? 'selected' : '' }}>USA</option>
<option value="grm" {{ (old('state') == 'grm') ? 'selected' : '' }}>Germany</option>
<option value="eng" {{ (old('state') == 'eng') ? 'selected' : '' }}>England</option>
</select>

但在这种形式中,我有另一个从Jquery文件中用于加载选项的select标记:

<select name="state" id="state" class="form-control arrow-black">
<option value="">Select country</option>
</select>

Jquery代码:

function loadlist(country){
with(document.getElementById('state')) 
{
options.length = 0;
if(country == ''){
options[0] = new Option('Select country' , '');
}
if (country == 'usa') {
options[0] = new Option('Select' , '');
options[1] = new Option('NewYork' , 'NewYork');
options[2] = new Option('Florida' , 'Florida');
options[3] = new Option('California' , 'California');
}
if (country == 'grm') {
options[0] = new Option('Select' , '');
options[1] = new Option('Bayern' , 'Bayern');
options[2] = new Option('Hamburg‎' , 'Hamburg‎');
options[3] = new Option('Berlin' , 'Berlin');
}
if (country == 'eng') {
options[0] = new Option('Select' , '');
options[1] = new Option('Buckingham' , 'Buckingham');
options[2] = new Option('Cambridge‎' , 'Cambridge‎');
options[3] = new Option('London' , 'London');
}
}
}

如何为第二个选择标签设置{{ (old('state') == 'The state name') ? 'selected' : '' }}

只需在页面加载时重新加载第二个选择:

$( document ).ready(loadlist($("select[name=country] " ). Val() ) );

最新更新