简体   繁体   English

使用onChange清除多个选择和复选框

[英]Clearing multiple selects and check-boxes using onChange

I have a form which has 3 select boxes, 8 check boxes and 1 text field. 我有一个包含3个选择框,8个复选框和1个文本字段的表单。

The second and third select boxes are populated by the prior selection: Select 1 fills select 2 select 2 fills select 3. 第二个和第三个选择框由先前的选择填充:选择1填充选择2选择2填充选择3。

When I change select 2, select 3 will clear... PERFECT! 当我更改选择2时,选择3将清除...完美! However, when I change select 1, only select 2 clears. 但是,当我更改选择1时,仅选择2会清除。 Select 3 still has a value. 选择3仍然有一个值。

<html>
<select id="subject_select" name="subjects" class="chosen-select" data-    placeholder="Select a Subject" style="width:100%;font-size:.85em;" onchange="subject(this.value)">
<option></option>
<option value="1"></option>
<option> ...
</select>
<div id="subject"><select id="subjects_select" class="chosen-select" data-placeholder="Select a Grade" style="width:100%;font-size:.85em;" ></select></div>    
<div id="grade"><select id="standards_select" class="chosen-select" multiple data-placeholder="Select Standard(s)"></select></div>

My javascript is: 我的JavaScript是:

function subject(str) {
    if (str == "") {
        return;
    } else { 
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("subject").innerHTML = xmlhttp.responseText;
                    $("#subjects_select").chosen();
                }
            }
            xmlhttp.open("GET","subject_get.php?subj="+str,true);
            xmlhttp.send();
        }
    }

I have tried using document.getElementById('standards_select').value = ""; 我已经尝试过使用document.getElementById('standards_select').value = ""; and function clear(val) {} as welll as onfocus="this.form.reset()" all to no avail. function clear(val) {}以及function clear(val) {} onfocus="this.form.reset()"都无效。

Can someone make a suggestion as to where I am going wrong? 有人可以对我要去哪里提出建议吗? Please do not answer the question outright. 请不要直接回答问题。

I think your example code is missing the relevant parts. 我认为您的示例代码缺少相关部分。 However, you might be able to fix it by triggering the change event manually when there's a change for subject : 但是,您可以通过在主题发生更改时手动触发change事件来解决此subject

document.getElementById('standards_select').dispatchEvent(new Event("change"));

This will additionally trigger your usual change listener for select 2 when select 1 is changed. 当选择1更改时,这还将触发您通常的选择2的更改侦听器。

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

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