Javascript 为什么不能设置第二个和第三个选择框的值
我有3个选择框。第二个选择框取决于第一个选择框中的选项。第三个选择框取决于第二个选择框中的选项。但在下面的代码中,我只为一个选项编写了一个选择框。问题是,如果我从数据库获取数据,我无法设置第二个和第三个选择框的值。在下面的代码中,我在一些变量中设置了值 我试过用这个:Javascript 为什么不能设置第二个和第三个选择框的值,javascript,jquery,html,Javascript,Jquery,Html,我有3个选择框。第二个选择框取决于第一个选择框中的选项。第三个选择框取决于第二个选择框中的选项。但在下面的代码中,我只为一个选项编写了一个选择框。问题是,如果我从数据库获取数据,我无法设置第二个和第三个选择框的值。在下面的代码中,我在一些变量中设置了值 我试过用这个: $("#k1").val(icd); $("#"+icd).show(); $("#"+icd).val(icd1); console.log($("#"+icd).val()); if (icd2 != "") { $
$("#k1").val(icd);
$("#"+icd).show();
$("#"+icd).val(icd1);
console.log($("#"+icd).val());
if (icd2 != "") {
$("#"+icd1).show();
$("#"+icd1).val(icd2);
}
但什么也没发生
以下是完整的代码:
var-icd=K02;
var icd1=K02-4;
var icd2=黑色素牙母细胞增生症;
$k1.5有效期;
$+icd.show;
$+icd.valicd1;
如果icd2!={
$+icd1.show;
$+icd1.2;
}
$k1.changefunction{
correspondingID=$this.find:selected.val
console.log1+对应的ID;
$.style-sub-1.hide;
$+对应的ID.show;
$+对应的ID.changefunction{
correspondingID2=$this.find:selected.val;
console.log2+对应的ID2;
$.style-sub-2.hide;
$+对应的ID2.show;
var nyari=$'div'.find+对应的ID2;
//var cari=查找$+对应ID2;
console.lognyari.length;
如果nyari.length==0{
对应的ID3=;
}否则{
$+对应的ID2.changefunction{
correspondingID3=$this.find:selected.val;
console.log3+对应的ID3;
};
}
};
};
皮利
K00牙齿发育和萌出障碍
K01埋伏阻生牙
K02龋齿
K03牙齿硬组织的其他疾病
K04牙髓和根尖周组织疾病
K05牙龈炎和牙周病
K06牙龈和无牙牙槽嵴的其他疾病
K07牙面异常[包括错牙合]
K08牙齿和支撑结构的其他疾病
K09口腔区域囊肿,未分类
K10其他颌骨疾病
K11涎腺疾病
K12口炎及相关病变
K13嘴唇和口腔粘膜的其他疾病
K14舌病
皮利
K02.0仅限于牙釉质的龋齿
K02.1牙本质龋病
K02.2牙骨质龋
K02.3被捕龋齿
K02.4牙折裂
K02.5其他龋齿
K02.6龋齿,未指明
皮利
白斑病变[最初龋齿]
皮利
婴儿黑牙症
黑色素牙母细胞增生
您在div上使用了id K02,在select上也使用了id K02,这导致了问题。因为当您尝试使用id选择DOM元素时,会返回顶部具有该id的第一个元素 这里是更新的代码
<select size="1" id="k1" class="form-control" type="select" name="kode_icd" value="-Select Your Rank-">
<option disabled selected value>pilih</option>
<option value="K00">K00 Disorders of tooth development and eruption</option>
<option value="K01">K01 Embedded and impacted teeth</option>
<option value="K02">K02 Dental caries</option>
<option value="K03">K03 Other diseases of hard tissues of teeth</option>
<option value="K04">K04 Diseases of pulp and periapical tissues</option>
<option value="K05">K05 Gingivitis and periodontal diseases</option>
<option value="K06">K06 Other disorders of gingiva and edentulous alveolar ridge</option>
<option value="K07">K07 Dentofacial anomalies [including malocclusion]</option>
<option value="K08">K08 Other disorders of teeth and supporting structures</option>
<option value="K09">K09 Cysts of oral region, not elsewhere classified</option>
<option value="K10">K10 Other diseases of jaws</option>
<option value="K11">K11 Diseases of salivary glands</option>
<option value="K12">K12 Stomatitis and related lesions</option>
<option value="K13">K13 Other diseases of lip and oral mucosa</option>
<option value="K14">K14 Diseases of tongue</option>
</select>
<div id = "K02" class="style-sub-1" style="display: none;" name="stylesub1">
<label> </label>
<select id="K02-select" name="kode_icd1" class="form-control">
<option disabled selected value>pilih</option>
<option value="K02-0">K02.0 Caries limited to enamel</option>
<option value="K02-1">K02.1 Caries of dentine</option>
<option value="K02-2">K02.2 Caries of cementum</option>
<option value="K02-3">K02.3 Arrested dental caries</option>
<option value="K02-4">K02.4 Odontoclasia</option>
<option value="K02-5">K02.5 Other dental caries</option>
<option value="K02-6">K02.6 Dental caries, unspecified</option>
</select>
</div>
<div id="K02-0" class="style-sub-2" style="display: none;" name="stylesub1">
<label> </label>
<select id="k02-0" name="kode_icd2" class="form-control">
<option disabled selected value>pilih</option>
<option value="White spot lesions [initial caries]">White spot lesions [initial caries]</option>
</select>
</div>
<div id="K02-4" class="style-sub-2" style="display: none;" name="stylesub1">
<label> </label>
<select id="k02-4" name="kode_icd2" class="form-control">
<option disabled selected value>pilih</option>
<option value="Infantile melanodontia">Infantile melanodontia</option>
<option value="Melanodontoclasia">Melanodontoclasia</option>
</select>
</div>
您可以复制fiddle代码并在此处创建一个代码段。change correspondingID2=$this.find:selected.val;to var correspondingID2=$this.find:selected.attr'id'@SharmilaJesupaul现在仍然无法选择值您只有K02的值,因此如果选择K02 Dental caries,您将获得第二次选择的下拉列表box@ChiragPatel我已经更新了这个问题
$(document).ready(function () {
var icd = "K02";
var icdS = "K02-select";
var icd1 = "K02-4";
var icd2 = "Melanodontoclasia";
$("#k1").val(icd);
$("#" + icd).show();
$("#" + icdS).val(icd1);
if (icd2 != "") {
$("#" + icd1).show();
$("#" + icd1).val(icd2);
}
$("#k1").change(function () {
correspondingID = $(this).find(":selected").val()
console.log("1" + correspondingID);
$(".style-sub-1").hide();
$("#" + correspondingID).show();
$("#" + correspondingID).change(function () {
correspondingID2 = $(this).find(":selected").val();
console.log("2" + correspondingID2);
$(".style-sub-2").hide();
$("#" + correspondingID2).show();
var nyari = $('div').find("#" + correspondingID2);
//var cari = find($("#"+correspondingID2));
console.log(nyari.length);
if (nyari.length == 0) {
correspondingID3 = "";
} else {
$("#" + correspondingID2).change(function () {
correspondingID3 = $(this).find(":selected").val();
console.log("3" + correspondingID3);
});
}
});
});
});