无法使用html表单和infobox.js显示默认文本

时间:2013-01-25 14:46:35

标签: javascript html-form infobox

网页设计师新手在这里,所以我希望这些条款是正确的,问题不是太尴尬。我得到的是一个包含4个项目的html表单(在html5文档中)(尽管会有更多):

  1. 选择一个页面
  2. Page 1
  3. Page 2
  4. Page 3
  5. ...以及这三个页面中的每一个的文本,都使用CSS隐藏。首次打开或刷新页面时,在表单菜单框中可以看到“选择页面”。当选择一个页面“infobox.js”(我多年前在网上的某个地方)时,该页面的文本在菜单框下方可见。效果很好。

    我想要的是:首次打开页面时,在表单菜单框中可以看到“选择页面”,并且页面1的文本已在其下方可见。

    我认为这是可能的,但我只是想把头发撕掉几天,并希望解决方案对某人来说真的很容易。在此先感谢您的帮助!

    HTML

    <form>
    <select id="someID_1" name="someID_1" onChange="selectForm1(this.options[this.selectedIndex].value)">
    <option>Choose a Page</option>
    <option value="1">Page 1</option>
    <option value="2">Page 2</option>
    <option value="3">Page 3</option>
    </select>
    </form>
    
    <div id="allForms1">
    <form class="hidden">Text of Page 1</form>
    <form class="hidden">Text of Page 2</form>
    <form class="hidden">Text of Page 3</form>
    </div>
    

    CSS

    .hidden { display: none; }
    

    INFOBOX.JS

    function selectForm1(frm){
    /* Select the div containing all the hidden forms */
      var hiddenForms1 = document.getElementById("allForms1");
    /* Select every form within the above div and assign it to an array */
      theForm1 = hiddenForms1.getElementsByTagName("form");
    /* Set the display for each of the above forms to NONE */
      for(x=0; x<theForm1.length; x++){
        theForm1[x].style.display = "none";
      }
    /* If the form selected from the list exists, set its display to BLOCK */
      if (theForm1[frm-1]){
        theForm1[frm-1].style.display = "block";
      }
    }
    

1 个答案:

答案 0 :(得分:0)

好的,我再次猜测后得到了它。这就是诀窍:

<body onload="selectForm1(1)">