Javascript 当父选择为空时,子选择列表中的默认值将消失
我有以下Jquery,它将我的选择列表的值附加到第二个选择列表中 JqueryJavascript 当父选择为空时,子选择列表中的默认值将消失,javascript,jquery,Javascript,Jquery,我有以下Jquery,它将我的选择列表的值附加到第二个选择列表中 Jquery $('#country').change(function() { var getid = $(".selectlist").val(); $('#countryid').html(''); $.each(getid, function(index, val) { $('#countryid').append($(val).clone());
$('#country').change(function() {
var getid = $(".selectlist").val();
$('#countryid').html('');
$.each(getid, function(index, val) {
$('#countryid').append($(val).clone());
});
});
我发现,如果我在父选择(国家)中取消选择项目,则子选择(国家ID)中的选项“选择国家代码”将消失。当var getid为空时,如何将其重新引入我的append
HTML
日本
大不列颠联合王国
选择国家代码
只需将其附加在$之前。each()
:
$('#country')。更改(函数(){
var getid=$(“.selectlist”).val();
$('#countryid').html('').append($('',{text:“选择国家代码”}));
$.each(getid,函数(index,val){
$('#countryid').append($(val.clone());
});
});
如果仅在没有选定项目时才需要它:
$('#country').change(function() {
var getid = $(".selectlist").val();
$('#countryid').html('');
$.each(getid, function(index, val) {
$('#countryid').append($(val).clone());
});
if (!getid)
$("#countryid").append($("<option/>", {text: "Choose Country Code"}));
});
$('#country')。更改(函数(){
var getid=$(“.selectlist”).val();
$('#countryid').html('');
$.each(getid,函数(index,val){
$('#countryid').append($(val.clone());
});
如果(!getid)
$(“#countryid”).append($(“”,{text:“选择国家代码”}));
});
$('#country').change(function() {
var getid = $(".selectlist").val();
$('#countryid').html('').append($("<option/>", {text: "Choose Country Code"}));
$.each(getid, function(index, val) {
$('#countryid').append($(val).clone());
});
});
$('#country').change(function() {
var getid = $(".selectlist").val();
$('#countryid').html('');
$.each(getid, function(index, val) {
$('#countryid').append($(val).clone());
});
if (!getid)
$("#countryid").append($("<option/>", {text: "Choose Country Code"}));
});