jQuery - 添加第二个变量到 select 选项字段
jQuery - Add a second var to select option field
我有以下 JS 代码:
//vars
var plz = jQuery('#plz_field_12374786');
var ortsteil = jQuery('#ortsteil_field_31289743');
var is_ortsteil = jQuery('#is_ortsteil_12312487');
//PLZ-Ajax for Ortsteil
jQuery(plz).on('change', function(){
var obj = jQuery(this);
obj.addClass('loading');
jQuery.ajax({
url: '/xxxxxxx/wp-admin/admin-ajax.php?action=get_ortsteil_from_plz&plz='+jQuery(this).val(),
context: document.body
}).done(function(data) {
var ortsteile = jQuery.parseJSON(data);
jQuery(ortsteil).find('option').each(function(){
jQuery(this).remove();
});
if(ortsteile.length>1){
for (var i=0;i<ortsteile.length;i++){
jQuery('<option/>').val(ortsteile[i]).html(ortsteile[i]).appendTo(ortsteil);
}
jQuery(ortsteil).show();
jQuery(ortsteil).attr('disabled', false);
jQuery(is_ortsteil).val('true');
}else{
jQuery(ortsteil).hide();
jQuery(ortsteil).attr('disabled', 'disabled');
jQuery(is_ortsteil).val('false');
}
obj.removeClass('loading');
});
});
结果是一个 select 字段,例如:
<select style="" class="ortsteil" id="ortsteil_field_31289743" name="ortsteil_field_31289743">
<option value="Districtname1">Districtname1</option>
<option value="Districtname2">Districtname2</option>
<option value="Districtname3">Districtname3</option>
</select>
我也想显示 PLZ(邮政编码)...喜欢:
<select style="" class="ortsteil" id="ortsteil_field_31289743" name="ortsteil_field_31289743">
<option value="PLZ - Districtname1">PLZ - Districtname1</option>
<option value="PLZ - Districtname2">PLZ - Districtname2</option>
<option value="PLZ - Districtname3">PLZ - Districtname3</option>
</select>
有人可以帮助我并告诉我我必须更改什么吗?
谢谢!!!!
变化:
jQuery('<option/>').val(ortsteile[i]).html(ortsteile[i]).appendTo(ortsteil);
到
jQuery('<option/>').val('PLZ - ' + ortsteile[i]).html('PLZ - ' + ortsteile[i]).appendTo(ortsteil);
或者如果PLZ -
是一个变量,用它替换上面的字符串:
jQuery('<option/>').val(plz + ' - ' + ortsteile[i]).html(plz + ' - ' + ortsteile[i]).appendTo(ortsteil);
我有以下 JS 代码:
//vars
var plz = jQuery('#plz_field_12374786');
var ortsteil = jQuery('#ortsteil_field_31289743');
var is_ortsteil = jQuery('#is_ortsteil_12312487');
//PLZ-Ajax for Ortsteil
jQuery(plz).on('change', function(){
var obj = jQuery(this);
obj.addClass('loading');
jQuery.ajax({
url: '/xxxxxxx/wp-admin/admin-ajax.php?action=get_ortsteil_from_plz&plz='+jQuery(this).val(),
context: document.body
}).done(function(data) {
var ortsteile = jQuery.parseJSON(data);
jQuery(ortsteil).find('option').each(function(){
jQuery(this).remove();
});
if(ortsteile.length>1){
for (var i=0;i<ortsteile.length;i++){
jQuery('<option/>').val(ortsteile[i]).html(ortsteile[i]).appendTo(ortsteil);
}
jQuery(ortsteil).show();
jQuery(ortsteil).attr('disabled', false);
jQuery(is_ortsteil).val('true');
}else{
jQuery(ortsteil).hide();
jQuery(ortsteil).attr('disabled', 'disabled');
jQuery(is_ortsteil).val('false');
}
obj.removeClass('loading');
});
});
结果是一个 select 字段,例如:
<select style="" class="ortsteil" id="ortsteil_field_31289743" name="ortsteil_field_31289743">
<option value="Districtname1">Districtname1</option>
<option value="Districtname2">Districtname2</option>
<option value="Districtname3">Districtname3</option>
</select>
我也想显示 PLZ(邮政编码)...喜欢:
<select style="" class="ortsteil" id="ortsteil_field_31289743" name="ortsteil_field_31289743">
<option value="PLZ - Districtname1">PLZ - Districtname1</option>
<option value="PLZ - Districtname2">PLZ - Districtname2</option>
<option value="PLZ - Districtname3">PLZ - Districtname3</option>
</select>
有人可以帮助我并告诉我我必须更改什么吗?
谢谢!!!!
变化:
jQuery('<option/>').val(ortsteile[i]).html(ortsteile[i]).appendTo(ortsteil);
到
jQuery('<option/>').val('PLZ - ' + ortsteile[i]).html('PLZ - ' + ortsteile[i]).appendTo(ortsteil);
或者如果PLZ -
是一个变量,用它替换上面的字符串:
jQuery('<option/>').val(plz + ' - ' + ortsteile[i]).html(plz + ' - ' + ortsteile[i]).appendTo(ortsteil);