1

我想根据组合框的选定值的值设置文本框的可见性。这是我的代码:

<table width="500" align="center">
  <tr>
    <td><span> Name:</span></td>
    <td><input name="name"  type="text" id="name" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Email ID:</span></td>
    <td><input name="email"  type="email" id="email" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Select:</span></td>
    <td>
      <select id="edition">
        <option value="none" selected>----Select ----</option>
        <option id="1">A</option>
        <option id="2">B</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><span>Number of Licenses</span></td>
    <td><input type="text" id="licenseNo" size="5" value="30" /></td>
  </tr>
</table>

licenseNo如果组合框的值为 B,我希望文本框可见。我不知道该怎么做。

4

2 回答 2

1

使用 JS 添加事件监听器:

<script type="text/javascript">
function func() {
    var elem = document.getElementById("edition");
    if(elem.options[elem.selectedIndex].innerHTML == "B") {
        document.getElementById("licenseNo").style.display = "";
    } else {
        document.getElementById("licenseNo").style.display = "none";
    }
}
</script>

<select id="edition" onchange="javascript:func();">
    ...
</select>

func()当您的页面刚刚加载时,也不要忘记在开始时调用。

于 2013-03-14T13:19:43.413 回答
0

您也可以像下面这样使用它。只需使用 value 来获取选定的文本

<table width="500" align="center" >
  <tr>
    <td><span> Name:</span></td>
    <td><input name="name"  type="text" id="name" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Email ID:</span></td>
    <td><input name="email"  type="email" id="email" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Select:</span></td>
    <td>
     <select id="edition" onchange="func()">
      <option value="none" selected >----Select ----</option>
      <option id="1">A</option>
      <option id="2">B</option>
     </select>
    </td>
  </tr>
  <tr id ="trhide">
    <td><span>Number of Licenses</span></td>
    <td><input type="text" id="licenseNo" size="5" value="30" /></td>
  </tr>
</table>

<script type="text/javascript">
 function func() {
   var elem = document.getElementById("edition");

   if(elem.value == "B") {
      document.getElementById("trhide").style.visibility = "visible"; 
   } else {
     document.getElementById("trhide").style.visibility = "hidden"; 
   }
 }
</script>

在这里看小提琴

于 2013-03-14T13:36:42.280 回答