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

标签 javascript jquery html

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

  <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/

相关文章:

html - 除第一行外,在表格周围放置边框

javascript - 在 JavaScript 中验证 11 位数字

c# - javascript 未在 ASP.NET MVC 中调用

javascript - 如何将Ajax返回的数据显示为HTML

jquery - 使用 bootstrap 或纯 css 设计表格/网格复选框组

获取背景颜色的 Javascript 代码不起作用

html - 检查元素时屏幕宽度和元素宽度不对齐?

javascript - 如何在 HTML5 Javascript Canvas Phonegap 游戏中读取和写入文本文件?

javascript - 钛合金 - 无法使用 id 禁用 Tabbedbar 内的标签

javascript - 使用 javascript/jQuery 获取父 div 中的鼠标位置