如果在菜单中使用javascript选择其他字段,则显示其他字段

时间:2012-06-06 20:09:31

标签: javascript forms

我正在尝试显示另一个字段来捕获用户输入,如果他们在选择菜单中选择其他字段。我的代码如下所示,当我在菜单中选择其他时,id otherJobType不会取消隐藏。谁能发现我哪里出错?提前谢谢。

<style type="text/css">
#otherJobType {
    display:none;
}
</style> 

<script type="text/javascript">
function jobType(value){
        if (value == 'Other') {
            document.getElementById('otherJobType').style.display = 'block';
        } else {
            document.getElementById('otherJobType').style.display = 'none';
        }
    }
</script>

<select id="jobType" name="jobType" onchange="jobType(this.value);">
    <option value="option 1" selected>Option 1</option>
    <option value="Other">Other</option>
</select>

<input name="otherJobType" id="otherJobType" type="text" size="50" value="Please specify job type" onblur="if (this.value=='') { this.value='Please specify job type' }" onclick="if (this.value=='Please specify job type') { this.value='' }" />

1 个答案:

答案 0 :(得分:0)

我最后通过将函数重命名为“workJobType”来修复此问题,但将选择菜单上的id和名称保留为“jobType”。如果这些中的任何一个与函数名称相同,则由于某种原因它不起作用。如果有人能解释为什么它不起作用,我很想听听。

这是stange,因为Musa发布的jsfiddle中的一切都运行正常,但是在我更改了我正在使用的函数名称之前,我的页面上还有很多工作。