0

当用户从列表中选择项目时,我如何隐藏标签,我有这个代码来隐藏输入文本,但是我如何隐藏该输入文本的标签?

<form name="myform">
<table>
<td>
<select name="one" onchange="if (this.selectedIndex==8){this.myform['other'].style.visibility='visible'}else {this.myform['other'].style.visibility='hidden'};">
<option value="" selected="selected">Select...</option>
<option value="1">1</option>
<option value="2">3</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="other">Other</option>
</select>
<label>Other</label><input type="textbox" name="other" style="visibility:hidden;"/>
</td>
</table>
</form>

我想隐藏<label>Other</label> 我怎么能做到这一点?

4

3 回答 3

0

更改您的选择标签并输入文本,如下所示。

<select name="one" onchange="if (this.selectedIndex=='other'){document.getElementById("otherText").style.visibility='visible'}else {document.getElementById("otherText").style.visibility='hidden'};">


<div id="otherText" style="visibility:hidden;"><label>Other</label><input type="textbox" name="other" /></div>

希望这可以帮助。

于 2013-08-06T04:40:30.367 回答
0

演示

你不能像这样匹配下拉列表的文本或值this.selectedIndex

将其更改this.selectedIndex == 8为与索引匹配

给文本框id="other"显示或隐藏它。

<select name="one" onchange="if (this.selectedIndex== 8){document.getElementById('other').style.visibility='visible'}else {document.getElementById('other').style.visibility='hidden'};">

更新的演示

为标签创建了新的 idl_other

<select name="one" onchange="if (this.selectedIndex== 8){document.getElementById('l_other').style.visibility='visible';
document.getElementById('other').style.visibility='visible'}else {document.getElementById('l_other').style.visibility='hidden';
document.getElementById('other').style.visibility='hidden'};">

演示

使用带有 id otherwrap的单个 divlabeltextbox在其中

<select name="one" onchange="if (this.selectedIndex== 8){
document.getElementById('other').style.visibility='visible'}else{
document.getElementById('other').style.visibility='hidden'}">
于 2013-08-06T04:43:36.633 回答
0
function OnChange(that) {            
        var lblOther = document.getElementById('lblOther');
        if (that.options[that.selectedIndex].text == 'Other') {                
            lblOther.style.display = "block";
        }
        else {               
            lblOther.style.display = "none";               
        };
    }

<form name="myform">
    <table>
        <td>
            <select name="one" onchange="OnChange(this);">
                <option value="" selected="selected">Select...</option>
                <option value="1">1</option>
                <option value="2">3</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="other">Other</option>
            </select>
            <label id="lblOther" style="display: block;">Other</label><input type="textbox" name="other" style="visibility: hidden;" />
        </td>
    </table>
</form>
于 2013-08-06T05:18:25.150 回答