简体   繁体   English

JavaScript onchange函数不起作用

[英]JavaScript onchange function doesn't work

This javascript is supposed to change the drop down's HTML as well as change the content of the corresponding div's. 该javascript应该更改下拉菜单的HTML以及更改相应div的内容。 The first click changes the dropdown html but you cant change it back as well as you lose the little arrow on the dropdown that let users know its a dropdown. 第一次单击会更改下拉html,但是您无法将其更改回去,并且丢失了下拉菜单上的小箭头,该箭头让用户知道其下拉菜单。 The populate checkboxes doesnt populate the div because i dont know how to make javascript let me put/remove check boxes 填充复选框不会填充div,因为我不知道如何制作javascript让我放置/删除复选框

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 = "";
        }
    }

the populateCheckBoxes is supposed to have these check boxes when the tables radio button is checked, and when the unpopulaeCheckBoxes radio button is checked the div will be empty. 选中表格单选按钮时,populateCheckBoxes应该具有这些复选框,而选中unpopulaeCheckBoxes单选按钮时,div将为空。 I dont have this code in anything yet because i believe its supposed to be in the javascript but pasting that in obviously doesnt work 我还没有任何代码,因为我相信它应该在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:

This html holds the drop down that is supposed to change and the 2 divs that are supposed to change 此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>

here is the working code, its pretty plain, but works the same way, http://codepen.io/MarkBond/pen/JdOZaw?editors=101 . 这是工作代码,非常简单,但工作方式相同, http://codepen.io/MarkBond/pen/JdOZaw?editors=101 BTW this is done with bootstrap 顺便说一句,这是通过引导完成的

Re-read this line: 重新阅读此行:

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

That's some pretty pointless logic. 那是一些毫无意义的逻辑。 The ID of an element that you find with the ID populateSelection is always going to be populateSelection . 您找到的ID为populateSelection的元素的ID始终将为populateSelection

You should be using value attributes, and proper event handling. 您应该使用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> 

Some reading material: 一些阅读材料:

Change your function to this 将功能更改为此

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 = "";
    }
}

Covering your whole Question, I think you want like this: 涵盖您的整个问题,我想您想要这样:

Updated the whole Answer: 更新了整个答案:

HTML 的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>

Removed content inside ul and dynamically adding it: 删除了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 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 工作:CODEPEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM