2013-03-28 2 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> 

감사합니다.

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

답변

0

가 텍스트 입력을 유지하지만, 단지 표시하고 선택이 변경 될 때 숨길 대신 다음을 수행 할 수 div1의

+0

그것은 괜찮아요하지만 사용자가 잘못 입력이 다른 한 후 제출하면 양식이 다시로드되지만 사용자가 사용자가 inputed 다른 제출 한 값을 볼 수 없습니다. –

+0

내 편집 참조 : 고정. – MrCode

+0

작동하지 않습니다. –

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> 
관련 문제