2013-01-16 55 views
0

我正在一個窗體中,我想採取選擇值,當用戶選擇是值,然後我想顯示一個文本框部分,但下面的代碼不適合我。 請告訴我哪裏錯了我在這裏:任何人都可以告訴我哪裏錯了這個PHP代碼,而選擇選項?

<select id="gap" name="gap" onclick="gap_textbox();"> 
    <option value='select'>Select</option> 
    <option value='yes'>Yes</option> 
    <option value='no'>No</option> 
</select> 

<input type="text" name="gap_box" id="gap_text_box" /> 

<script type="text/javascript"> 
    function gap_textbox() { 
     alert ("am here" + " " +document.getElementById("gap").value); 
     if (document.getElementById("gap").value =='select') { 
      alert ("in value = select"); 
      document.getElementById("gap_text_box").disable=true; 
     } 
     else if (document.getElementById("gap").value =='no') { 
      alert ("in value = no"); 
      document.getElementById("gap_text_box").disable=true; 
     } else { 
      alert ("in value = yes"); 
      document.getElementById("gap_text_box").disable=false; 
     } 
    } 
</script> 
+0

使用以下功能。 – Sahal

回答

0

在下面一行...

<select id="gap" name="gap" onclick="gap_textbox();"> 

...你需要使用onchange而不是onclick

但是,使用內聯點擊處理程序被認爲是過時的並且難以維護。您應該使用合適的JavaScript事件處理...

document.getElementById("gap").onchange = function() { 
    gap_textbox() 
}; 

或者,更好的是,使用庫,如jQuery ...下面的代碼

$('#gap').change(function() { 
    gap_textbox(); 
}); 
0

嘗試。只做出改變,用onchange代替onclick函數。對於選擇框,您必須使用onChange函數。我們沒有點擊選擇框中的任何內容。

<select id="gap" name="gap" onchange="gap_textbox();"> 
<option value='select'>Select</option> 
<option value='yes'>Yes</option> 
<option value='no'>No</option> 
</select> 
<input type="text" name="gap_box" id="gap_text_box" /> 
<script type="text/javascript"> 
function gap_textbox() 
{ 
    alert ("am here" + " " +document.getElementById("gap").value); 
    if (document.getElementById("gap").value =='select') 
    { 
    alert ("in value = select"); 
    document.getElementById("gap_text_box").disable=true; 
    } 
    else if (document.getElementById("gap").value =='no') 
    { 
    alert ("in value = no"); 
    document.getElementById("gap_text_box").disable=true; 
    } 
    else 
    { 
    alert ("in value = yes"); 
    document.getElementById("gap_text_box").disable=false; 
    } 
} 
</script> 
相關問題