1

这是html代码..

<select id="dept" name="dept" style="float: left;">
        <option value="eee" onclick="toggleDisplay('dept_input_div', 0)">EEE</option>
        <option value="ece" onclick="toggleDisplay('dept_input_div', 0)">ECE</option>
        <option value="eie" onclick="toggleDisplay('dept_input_div', 0)">EIE</option>
        <option value="cse" onclick="toggleDisplay('dept_input_div', 0)">CSE</option>
        <option value="it" onclick="toggleDisplay('dept_input_div', 0)">IT</option>
        <option value="mechanical" onclick="toggleDisplay('dept_input_div', 0)">Mechanical</option>
        <option value="civil" onclick="toggleDisplay('dept_input_div', 0)">Civil</option>
        <option value="mechatronics" onclick="toggleDisplay('dept_input_div', 0)">Mechatronics</option>
        <option value="biotech" onclick="toggleDisplay('dept_input_div', 0)">Bio-tech</option>
        <option value="automobile" onclick="toggleDisplay('dept_input_div', 0)">Automobile</option>
        <option value="chemical" onclick="toggleDisplay('dept_input_div', 0)">Chemical</option>
        <option value="aeronautical" onclick="toggleDisplay('dept_input_div', 0)">Aeronautical</option>
        <option value="architecture" onclick="toggleDisplay('dept_input_div', 0)">Architecture</option>
        <option value="others" onclick="javascript:toggleDisplay('dept_input_div', 1);">Others..</option>
      </select>
      <div id="dept_input_div" style="display: none">
      <input class="inputbox" name="dept-input" type="text" id="dept-inputbox" />
      </div>

这是javascript代码..

    function toggleDisplay(id, displayvalue)
{
    if ( displayvalue == 1 ) {
        document.getElementById(id).style.display = 'block';
    } else if ( displayvalue == 0 ) {
        document.getElementById(id).style.display = 'none';
    }
}

上面的代码在 Firefox 和 ie(兼容性视图已关闭)中运行良好,但在 chrome 和 ie(兼容性已打开)中运行良好。

我究竟做错了什么?有没有一种适用于所有浏览器的正确方法?

4

1 回答 1

2

使用 onchange 而不是 onclick

html

<select id="dept" name="dept" style="float: left;" onchange="toggleDisplay('dept_input_div' , this.value)">
            <option value="eee" >EEE</option>
            <option value="ece" >ECE</option>
            <option value="eie" >EIE</option>
            <option value="cse" >CSE</option>
            <option value="it" >IT</option>
            <option value="mechanical" >Mechanical</option>
            <option value="civil" >Civil</option>
            <option value="mechatronics" >Mechatronics</option>
            <option value="biotech" >Bio-tech</option>
            <option value="automobile" >Automobile</option>
            <option value="chemical" >Chemical</option>
            <option value="aeronautical" >Aeronautical</option>
            <option value="architecture" >Architecture</option>
            <option value="others">Others..</option>
</select>
<div id="dept_input_div" style="display: none">
      <input class="inputbox" name="dept-input" type="text" id="dept-inputbox" />
</div>

javascript

function toggleDisplay(id, displayvalue)
{    
    if ( displayvalue == "others" ) {
         document.getElementById(id).style.display = 'block';
    } else {
         document.getElementById(id).style.display = 'none';
    }
}
于 2013-01-08T06:02:59.000 回答