JavaScript onchange功能不起作用

时间:2015-06-24 20:13:22

标签: javascript jquery html twitter-bootstrap onchange

这个javascript应该更改下拉列表的HTML以及更改相应div的内容。第一次点击会更改下拉列表html,但您无法将其更改回来,并且您丢失了下拉列表中的小箭头,让用户知道它的下拉列表。填充复选框不会填充div,因为我不知道如何使javascript让我放/删除复选框

JAVASCRIPT:

    function changeSelection() {
        var x = document.getElementById("populateSelection").id;

        if (x == "selectionViews") {
            document.getElementById("dropdownMenuSelect").innerHTML = "Views";
            document.getElementById("populateCheckBoxes").innerHTML = "";
        } else {
            document.getElementById("dropdownMenuSelect").innerHTML = "Tables";
            document.getElementById("unpopulateCheckBoxes").innerHTML = "";
        }
    }

当选中表格单选按钮时,populateCheckBoxes应该有这些复选框,当选中unpopulaeCheckBoxes单选按钮时,div将为空。我还没有这个代码,因为我相信它应该在javascript中但是粘贴它显然不起作用

                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="selectionCondition" checked/>50 million
                            </label>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="selectionDistribution"/>100 million
                            </label>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="selectionProgram"/>Status Quo
                            </label>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="selectionTreatment"/>Do Nothing
                            </label>
                        </div>

HTML:

这个html包含应该更改的下拉列表以及应该更改的2个div

                    <form role="form">
                        <div class="row">
                            <div class="radio col-xs-2" id="populateSelection"  onchange="changeSelection()">
                                <label>
                                    <input type="radio" name="optradio" id="selectionTables" />Use Tables
                                </label>&nbsp;&nbsp;
                                <label>
                                    <input type="radio" name="optradio" id="selectionViews" />Use Views
                                </label>
                            </div>
                            <div class="dropdown col-xs-10">
                                <!--DROPDOWN BUTTON-->
                                <button class="btn btn-default dropdown-toggle btn-xs btn-orange" type="button" id="dropdownMenuSelect" data-toggle="dropdown" aria-expanded="true" style="margin-top:10px">
                                    Views
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuSelect">
                                    <!--DROPDOWN MENU-->
                                    <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneChart">Chart</a></li>
                                    <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneData">Data</a></li>
                                </ul>
                            </div>
                        </div>
                        <div class="row" id="populateCheckBoxes"></div>
                        <div class="row" id="unpopulateCheckBoxes"></div>
                    </form>

这是工作代码,非常简单,但工作方式相同,http://codepen.io/MarkBond/pen/JdOZaw?editors=101。顺便说一句,这是用bootstrap

完成的

3 个答案:

答案 0 :(得分:2)

重读这一行:

var x = document.getElementById("populateSelection").id;

这是一些非常毫无意义的逻辑。您使用ID populateSelection找到的元素的ID始终为populateSelection

您应该使用value属性和正确的事件处理。

DEMO

function changeSelection(e) {
  var x = e.target.value;
  if (x == "selectionViews") {
    document.getElementById("dropdownMenuSelect").innerHTML = "Views";
    document.getElementById("populateCheckBoxes").innerHTML = "";
  } else {
    document.getElementById("dropdownMenuSelect").innerHTML = "Tables";
    document.getElementById("unpopulateCheckBoxes").innerHTML = "";
  }
}

document.getElementById('populateSelection').addEventListener('change', changeSelection);
<!-- Latest compiled and minified CSS --><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<form role="form">
  <div class="row">
    <div class="radio col-xs-2" id="populateSelection">
      <label>
        <input type="radio" name="optradio" value="selectionTables" />Use Tables
      </label>&nbsp;&nbsp;
      <label>
        <input type="radio" name="optradio" value="selectionViews" />Use Views
      </label>
    </div>
    <div class="dropdown col-xs-10">
      <!--DROPDOWN BUTTON-->
      <button class="btn btn-default dropdown-toggle btn-xs btn-orange" type="button" id="dropdownMenuSelect" data-toggle="dropdown" aria-expanded="true" style="margin-top:10px">
        Views
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuSelect">
        <!--DROPDOWN MENU-->
        <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneChart">Chart</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneData">Data</a></li>
      </ul>
    </div>
  </div>
  <div class="row" id="populateCheckBoxes"></div>
  <div class="row" id="unpopulateCheckBoxes"></div>
</form>

一些阅读材料:

答案 1 :(得分:2)

将您的功能更改为此

function changeSelection() {

    if (document.getElementById("selectionViews").checked) {
        document.getElementById("dropdownMenuSelect").innerHTML = "Views";
        document.getElementById("populateCheckBoxes").innerHTML = "";
    } else {
        document.getElementById("dropdownMenuSelect").innerHTML = "Tables";
        document.getElementById("unpopulateCheckBoxes").innerHTML = "";
    }
}

答案 2 :(得分:2)

涵盖整个问题,我想你想要这样:

更新了整个答案:

<强> HTML

<div class="radio col-xs-2" id="populateSelection" onchange="changeSelection()">
      <label>
        <input type="radio" name="optradio" id="selectionTables" />Use Tables
      </label>&nbsp;&nbsp;
      <label>
        <input type="radio" name="optradio" id="selectionViews" />Use Views
      </label>
    </div>

删除ul内的内容并动态添加:

<div class="dropdown col-xs-10">
      <!--DROPDOWN BUTTON-->
      <button class="btn btn-default dropdown-toggle btn-xs btn-orange" type="button" id="dropdownMenuSelect" data-toggle="dropdown" aria-expanded="true" style="margin-top:10px">
        Views
        <span class="caret"></span>
      </button>
      <ul id="dropdown-menu" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuSelect"></ul>
    </div>

<强> JS / JQuery的

function changeSelection() {

      if (document.getElementById("selectionTables").checked) {
        var x = document.getElementById("dropdownMenuSelect");
        var text = "Tables ";
        text += "<span class='caret'>";
        text += "</span>";
        x.innerHTML = text;
        document.getElementById("populateCheckBoxes").innerHTML = "";
        var y = document.getElementById("dropdown-menu");
        var text2 = "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Chart 1";
        text2 += "</a></li";
        text2 += "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Data 1";
        text2 += "</a></li";
        y.innerHTML = text2;
      } else {
        var x = document.getElementById("dropdownMenuSelect");
        var text = "Views ";
        text += "<span class='caret'>";
        text += "</span>";
        x.innerHTML = text;
        document.getElementById("unpopulateCheckBoxes").innerHTML = "";
        var y = document.getElementById("dropdown-menu");
        var text2 = "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Chart 2";
        text2 += "</a></li";
        text2 += "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Data 2";
        text2 += "</a></li";
        y.innerHTML = text2;
      }
    }

WORKING:CODEPEN

相关问题