JavaScript在选择更改时不显示文本区域

时间:2012-08-20 11:52:43

标签: javascript html

function otherShow() {
    var textarea = document.getElementById("hideme");
    var x = document.forms.myForm.other_trade.value;
    if (x == "other") {
        textarea.style.display = 'block';
    }else {
        textarea.style.display = 'none';
    }
}

这是我的JavaScript代码,用于隐藏包含ID=hideme的文本区域并触发操作onchange="otherShow();"

<select name="trade" required="required" class="inputbox" id="trade" onchange="otherShow();">
<option value="" selected="selected"> Select one... </option>
<option value="" disabled="disabled">  ----------------------   </option>
<option value="General Labourer">Option1</option> 
.......
</select>

上面是Select,下面是Textarea

<textarea cols="36" rows="6" class="inputbox" id="hideme" name="other_trade"></textarea>

在选择的结尾我有一个

<option value="Other"> Other </other> 

我想在选择其他时显示文本区域。请帮助我认为loginc是正确的,但是当我将其更改为隐藏文本区域的某个值时它只是不起作用,但是不要更改它...

3 个答案:

答案 0 :(得分:1)

选择框的名称为trade而不是other_trade

var x = document.forms.myForm.trade.value;

或:

var x = this.value;

而不是:

var x = document.forms.myForm.other_trade.value;

答案 1 :(得分:1)

我将如何做到这一点

window.onload=function() {
  document.getElementById("trade").onchange=function() {
    var textarea = this.form.other_trade;
    textarea.style.display=(this.value=="Other")?"block":"none";
  }
}

答案 2 :(得分:1)

在您的javascript显示/隐藏条件中将其他替换为其他,因为它是比较案例敏感的

相关问题