单击选择选项时如何显示/隐藏div

时间:2012-04-21 03:38:32

标签: javascript html

我有一个选择框,想要在点击选项时在隐藏的div中显示帮助文本:

<select  onchange="optionCheck()" multiple="multiple" name="test" id="options" size="15" class="form-select">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<div id="help1" style=visibility:hidden>help text 1</div>
<div id="help2" style=visibility:hidden>help text 2</div>

<script type="text/javascript">
    function optionCheck(){
        var option = document.getElementById("options").value;
        if(option == "1"){
            document.getElementById("help1").style.visibility ="visible";
            document.getElementById("help2").style.visibility ="hidden";
        }
        if(option == "2"){
            document.getElementById("help2").style.visibility ="visible";
            document.getElementById("help1").style.visibility ="hidden";
        }
    }
</script>

如何创建一个简单的循环,这样我就不必为每个选项编写“if”语句了?

 document.getElementById(option).style.visibility ="visible";

无效 - javascript和我相处得不好; - )

另外,当选择一个选项时,是否有一种隐藏所有其他div的简单方法?

6 个答案:

答案 0 :(得分:4)

我建议使用一些CSS类来隐藏/显示div。然后JavaScript只需要为每个div设置class属性。

如果您选择多个选项,此解决方案仍然有效。

HTML:

<select onchange="optionCheck()" multiple="multiple" name="test" id="options" size="15" class="form-select">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<div id="help1" class="helpText">help text 1</div>
<div id="help2" class="helpText">help text 2</div>

CSS:

/* consider "display: none/block" instead of visibility: hidden/visible
   if you don't want the hidden divs to occupy space */
.helpText {
    visibility: hidden;
}
.helpTextShow {
    visibility: visible;
}​

JavaScript的:

function optionCheck() {
    var i, len, optionVal, helpDiv,
        selectOptions = document.getElementById("options");

    // loop through the options in case there
    // are multiple selected values
    for (i = 0, len = selectOptions.options.length; i < len; i++) {

        // get the selected option value
        optionVal = selectOptions.options[i].value;

        // find the corresponding help div
        helpDiv = document.getElementById("help" + optionVal);

        // move on if we didn't find one
        if (!helpDiv) { continue; }

        // set CSS classes to show/hide help div
        if (selectOptions.options[i].selected) {
            helpDiv.className = "helpText helpTextShow";
        } else {
            helpDiv.className = "helpText";
        }
    }    
}

// alternative method of binding the onchange handler, merely a suggestion
//document.getElementById("options").onchange = optionCheck;

这是一个证明:http://jsfiddle.net/willslab/3N9pm/9/

的jsFiddle

我希望有所帮助!

答案 1 :(得分:2)

请参阅修改后的HTML和JavaScript代码:

<强> HTML:

<div id="help1" style="display:none">help text 1</div>
<div id="help2" style="display:none">help text 2</div>

<强> JAVASCRIPT:

function optionCheck()
{
    var option = document.getElementById("options").value;
    if(option == "1"){
        document.getElementById("help1").style.display ="";
        document.getElementById("help2").style.display ="none";
    }
    if(option == "2"){
        document.getElementById("help2").style.display ="";
        document.getElementById("help1").style.display ="none";
    }
}

答案 2 :(得分:2)

我是这样做的:

首先,我很懒,我不想输入style =“visibility:hidden;”在每个div上。使用CSS一次隐藏所有这些更容易:

HTML:

<select  onchange="optionCheck()" multiple="multiple" name="test" id="options" size="15" class="form-select">
  <option value="1">option 1</option>
  <option value="2">option 2</option>
</select>

<div id="help-wrapper">
  <div id="help1">help text 1</div>
  <div id="help2">help text 2</div>
</div>

CSS:

#help-wrapper div {
  visibility: hidden;
}

/* It's better to use a class to highlight the selection,
   in case you feel like adding more styles later (like a border or something).
 */
.current-help {
  visibility: visible;
}

现在我们可以努力使Javascript变得更通用了:

function optionCheck()
{
  var i;
  var optionIndex = document.getElementById("options").value;

  var helpWrapper = document.getElementById("help-wrapper");
  var helpDivs = helpWrapper.getElementsByTagName("div");

  // Loop through all the help divs.
  for (i=0; i<helpDivs.length; i++) {
    // Parse out the numeric portion of this help div's id.
    var helpIndex = helpDivs[i].substring[4];

    // If we found our div, highlight it.
    if (helpIndex === optionIndex) {
      helpDivs[i].className = "current-help";
    }
    // Otherwise, we should make sure it's hidden,
    // in case it was the previous selection.
    else {
      helpDivs[i].className = "";
    } 
  }
}

这应该可以解决问题!

如果您对上述代码有任何疑问,我很乐意回答。

答案 3 :(得分:1)

最好用引号包装css样式:

  <div id="help1" style="visibility:hidden">help text 1</div>
  <div id="help2" style="visibility:hidden">help text 2</div>


function optionCheck(){
        var option = document.getElementById("options").value;
        document.getElementById("help" + option).style.visibility ="visible";
        }
}

答案 4 :(得分:0)

而不是getElementById(option),请尝试使用getElementById("help" + option)

答案 5 :(得分:0)

您可以在javascript中尝试使用三元运算符:

document.getElementById("options").value == 1?(document.getElementById("help1").style.visibility ="visible",document.getElementById("help2").style.visibility ="hidden"):(document.getElementById("help2").style.visibility ="visible",document.getElementById("help1").style.visibility ="hidden");

这将避免多个if语句