2

我有一个选择框,当您单击一个选项时,希望在隐藏的 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吗?

4

6 回答 6

4

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

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

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;

​</p>

这是一个 jsFiddle 来演示:http: //jsfiddle.net/willslab/3N9pm/9/

我希望这会有所帮助!

于 2012-04-21T04:11:15.820 回答
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";
    }
}
于 2012-04-21T04:02:16.050 回答
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 = "";
    } 
  }
}

这应该可以解决问题!

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

于 2012-04-21T07:10:38.617 回答
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";
        }
}
于 2012-04-21T03:59:55.730 回答
0

而不是getElementById(option),尝试类似的东西getElementById("help" + option)

于 2012-04-21T03:42:54.140 回答
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 语句

于 2012-04-21T03:53:57.003 回答