我正在一个窗体上工作,我想采取选择值,当用户选择是值,然后我想显示一个文本框部分,但以下代码不适合我。
<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>
下一行…
<select id="gap" name="gap" onclick="gap_textbox();">
…您需要使用onchange
而不是onclick
。
然而,使用内联点击处理程序被认为是过时的,难以维护。你应该使用正确的JavaScript事件处理…
document.getElementById("gap").onchange = function() {
gap_textbox()
};
或者,更好的是,使用库,如jQuery…
$('#gap').change(function() {
gap_textbox();
});
试试下面的代码。唯一的变化是将onclick
功能替换为onchange
。对于选择框,必须使用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>