gpt4 book ai didi

javascript - 通过更改下拉列表更改输入字段

转载 作者:行者123 更新时间:2023-11-30 20:45:21 25 4
gpt4 key购买 nike

我试图通过更改下拉值来更改表单输入字段..现在我找到了只有一个下拉选择的解决方案..即,如果我选择一个下拉值,一些请求的输入值正在获得..但不明白如何为每个下拉值获得不同的输入值..在这里我给出了编码我正在使用的..

  <script type="text/javascript">
$(function () {
$("#ddlPassport").change(function () {
if ($(this).val() == "airtel") {
$("#airtel").show();
} else {
$("#airtel").hide();
}
});
});

$(function () {
$("#ddlPassport").change(function () {
if ($(this).val() == "dishtv") {
$("#dishtv").show();
} else {
$("#dishtv").hide();
}

});
});
</script>
<select id="ddlPassport">
<option title="Airtel DTH" value="airtel" id="">Airtel DTH</option>
<option title="Reliance" value="reliance" id="">Big TV/Reliance Digital TV</option>
<option title="SUNTV" value="suntv">SUN TV</option>
<option title="Videocon" value="videocon">Videocon D2H</option>
<option title="DISHTV" value="dishtv">DISH TV</option>
<option title="Tata Sky" value="tatasky">TATA SKY</option>

</select>
<div class="form-group form-group-icon-left" id="airtel" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Customer ID :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Customer ID">
</div>
<div class="form-group form-group-icon-left" id="dishtv" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Mobile No. or Card No. :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Mobile No. or Card No.">
</div>
<div class="form-group form-group-icon-left" id="reliance-sun" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Smart Card No :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Smart Card No.">
</div>

最佳答案

查看您的代码,我发现了几个问题。

问题一

$("#ddlPassport").change()

您选择的标签没有 ID dllPassport,它是空的。

问题 2页面上有多个 HTML ID。

来自 XHTML 1.0 Spec

In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above. See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html.

您正在尝试显示/隐藏 ID 为 dishtv 的元素,该元素适用于两者

<option title="Reliance" value="reliance" id="dishtv">

<div class="form-group form-group-icon-left" id="dishtv" style="display:none;">

问题 3解决问题 1 和问题 2 后,您的 div 将显示 dishtv 的 ID,但是此 div 嵌套在父 div 中,并且还具有 display:none,因此您需要调用 。 show() 在这个 div 上还有:#airtel

编辑

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ddlPassport").change(function () {
// Airtel
if ($(this).val() == "airtel") {
$("#airtel").show();
} else {
$("#airtel").hide();
}

//DishTV
if ($(this).val() == "dishtv") {
$("#dishtv").show();
} else {
$("#dishtv").hide();
}
});
});

</script>
<select id="ddlPassport">
<option title="Airtel DTH" value="airtel" id="">Airtel DTH</option>
<option title="Reliance" value="reliance" id="">Big TV/Reliance Digital TV</option>
<option title="SUNTV" value="suntv">SUN TV</option>
<option title="Videocon" value="videocon">Videocon D2H</option>
<option title="DISHTV" value="dishtv">DISH TV</option>
<option title="Tata Sky" value="tatasky">TATA SKY</option>

</select>
<div class="form-group form-group-icon-left" id="airtel" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Customer ID :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Customer ID">
</div>
<div class="form-group form-group-icon-left" id="dishtv" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Mobile No. or Card No. :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Mobile No. or Card No.">
</div>
<div class="form-group form-group-icon-left" id="reliance-sun" style="display:none;">
<i class="fa fa-pencil input-icon input-icon-highlight"></i>
<label for="txtMobileNo"><span id="">Smart Card No :</span></label>
<input type="text" class="form-control" name="txtMobileNo" id="" placeholder="Smart Card No.">
</div>

关于javascript - 通过更改下拉列表更改输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com