2013-03-28 20 views
0

我有一個選擇項目的HTML列表。有一個值名稱:其他,如果選中,我想顯示一個文本輸入。Html可選框選定問題

以下是我的代碼。如果用戶選擇除other之外的任何選項,則應該隱藏文本輸入。當用戶從選擇中選擇other時,如何顯示此文本字段。

<td> 
    Interest <span style="color:#F00;">*</span> 
</td> 
<td> 
    <select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)"> 
     <option value="art" <?php if ($g_interest=="art") echo 'selected = "selected"'; ?>>Art</option> 
     <option value="litteratures" <?php if ($g_interest=="litteratures") echo 'selected = "selected"'; ?>>Litteratures</option> 
     <option value="business" <?php if($g_interest=="business") echo 'selected = "selected"'; ?>>Business</option> 
     <option value="internet" <?php if($g_interest=="internet") echo 'selected = "selected"'; ?>>Internet</option> 
     <option value="other_interest" <?php if($g_interest=="internet") echo 'selected = "selected"'; ?>>Other</option> 
    </select> 

    <script type="text/javascript"> 
     function showfield(name) { 
      if (name == 'other_interest') document.getElementById('div1').innerHTML = '<input type="text" name="other_interest" class="trother" />'; 
      else document.getElementById('div1').innerHTML = ''; 
     } 
    </script> 
    <div id="div1"></div> 
</td> 

謝謝。

回答

0

保持文字輸入有,但只是顯示和隱藏它,當選擇改變。

<select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)"> 
    <option value="art" <?php if ($g_interest=="art") echo 'selected = "selected"'; ?>>Art</option> 
    <option value="litteratures" <?php if ($g_interest=="litteratures") echo 'selected = "selected"'; ?>>Litteratures</option> 
    <option value="business" <?php if($g_interest=="business") echo 'selected = "selected"'; ?>>Business</option> 
    <option value="internet" <?php if($g_interest=="internet") echo 'selected = "selected"'; ?>>Internet</option> 
    <option value="other_interest" <?php if($g_interest=="other_interest") echo 'selected = "selected"'; ?>>Other</option> 
</select> 

<input type="text" id="other_interest" name="other_interest" class="trother" style="<?php if($g_interest != 'other_interest') echo 'display:none;'; ?>" /> 

<script type="text/javascript"> 
    function showfield(name) { 

     var input = document.getElementById("other_interest"); 
     if (name == 'other_interest') { 
      input.style.display = ""; 
     } else { 
      input.style.display = "none"; 
     } 
    } 
</script> 
+0

這很好,但如果用戶錯誤地輸入其他字段,然後表單被重新加載,但用戶無法看到用戶輸入的其他字段值。 – 2013-03-28 10:03:42

+0

看我的編輯:固定的。 – MrCode 2013-03-28 10:08:07

+0

這不起作用。 – 2013-03-28 10:11:33

0

而不是你的DIV1,你可以做以下的:

<select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)"> 
     <option value="art" <?php if ($g_interest=="art") echo 'selected = "selected"'; ?>>Art</option> 
     <option value="litteratures" <?php if ($g_interest=="litteratures") echo 'selected = "selected"'; ?>>Litteratures</option> 
     <option value="business" <?php if($g_interest=="business") echo 'selected = "selected"'; ?>>Business</option> 
     <option value="internet" <?php if($g_interest=="internet") echo 'selected = "selected"'; ?>>Internet</option> 
     <option value="other_interest" <?php if($g_interest=="internet") echo 'selected = "selected"'; ?>>Other</option> 
</select> 

<input type="text" name="other_interest" id="txtOther" class="trother" style="display: none;" /> 

<script> 
    function showfield(name) { 
     if (name == 'other_interest') document.getElementById('txtOther').style = 'display: block;'; 
     else document.getElementById('txtOther').style = 'display: none;'; 
    } 
</script>