javascript使用普通JS基于另一个multiselect框中的选择禁用multiselect

javascript使用普通JS基于另一个multiselect框中的选择禁用multiselect,javascript,html,Javascript,Html,我有两个多选框,一个用于国家列表,一个用于州列表 <select multiple="multiple" name="country[]" id="country" > <option value="0">Select a Country</option> <?php foreach($country_list as $key=>$value){ echo "<option value=\"$key\""

我有两个多选框,一个用于国家列表,一个用于州列表

<select multiple="multiple" name="country[]" id="country" >
      <option value="0">Select a Country</option>


  <?php 
    foreach($country_list as $key=>$value){
    echo "<option value=\"$key\"";
      if($html['Country Name']==$key|| $row['Country Name']==$key){
       echo ' selected="selected"';
      }
    echo ">$value</option>\n";                                     
    }?>
</select>


<select multiple="multiple" name="state[]" id="state">
      <option value="">Select a State</option>
      <?php 
        foreach($state_list as $key=>$value){
        echo "<option value=\"$key\"";
          if($html['state']==$key|| $row['state']==$key){
            echo ' selected="selected"';
          }
        echo ">$value</option>\n";                                     

        }?>
    </select>

当选择美国以外的国家/地区时,此代码禁用选择框。我希望它被禁用,即使在第一个下拉列表中选择了多个国家(如果用户选择美国和CAN,它也应该被禁用)。任何建议。我有Jquery中的工作代码,但我的服务器似乎根本不支持Jquery,我希望它使用传统的JS

这里有一些代码可以帮助您。您必须遍历所有选项元素,检查它们是否被选中。如果是,那么你需要将其与“美国”匹配-

鉴于此HTML-

<select id="country" multiple="multiple">
    <option value="usa">usa</option>
    <option value="canada">canada</option>
    <option value="israel">israel</option>
</select>

美国
加拿大
以色列
你会这样做吗-

var selectCountry = document.getElementById('country');

var usaIsMarked = false;
selectCountry.onchange = function () {
    usaIsMarked = false;
    for (i = 0; i < selectCountry.options.length; i++) {
        var currentOption = selectCountry.options[i];
        if (currentOption.selected && currentOption.value == 'usa') {
            usaIsMarked = true;
        }
    }
    if (usaIsMarked) {
        alert("usa was marked!");
    }
};
var selectCountry=document.getElementById('country');
var usaIsMarked=假;
选择country.onchange=函数(){
usaIsMarked=false;
用于(i=0;i

这里有一些代码可以帮助您。您必须遍历所有选项元素,检查它们是否被选中。如果是,那么你需要将其与“美国”匹配-

鉴于此HTML-

<select id="country" multiple="multiple">
    <option value="usa">usa</option>
    <option value="canada">canada</option>
    <option value="israel">israel</option>
</select>

美国
加拿大
以色列
你会这样做吗-

var selectCountry = document.getElementById('country');

var usaIsMarked = false;
selectCountry.onchange = function () {
    usaIsMarked = false;
    for (i = 0; i < selectCountry.options.length; i++) {
        var currentOption = selectCountry.options[i];
        if (currentOption.selected && currentOption.value == 'usa') {
            usaIsMarked = true;
        }
    }
    if (usaIsMarked) {
        alert("usa was marked!");
    }
};
var selectCountry=document.getElementById('country');
var usaIsMarked=假;
选择country.onchange=函数(){
usaIsMarked=false;
用于(i=0;i

只需浏览所有选定选项,并检查其中一个选项的值是否等于USA:

selectCountry.onchange = function() {
        var disabled = false;
        for(var i = 0; i < this.options.length; i++){
            if(this.options[i].selected && this.options[i].value == 'USA') {
                disabled =true;
                break
            }
        }
        document.getElementById('state').disabled = disabled;
    };

只需浏览所有选定选项,并检查其中一个选项的值是否等于USA:

selectCountry.onchange = function() {
        var disabled = false;
        for(var i = 0; i < this.options.length; i++){
            if(this.options[i].selected && this.options[i].value == 'USA') {
                disabled =true;
                break
            }
        }
        document.getElementById('state').disabled = disabled;
    };

为了测试是否选择了多个选项,您需要在选项之间循环并测试
selected
属性:

window.onload = function() {
    var selectCountry = document.getElementById('country'),
        selectState = document.getElementById('state');

    selectCountry.onchange = function() {
        var options = this.options,
            selected = 0, i, o;

        // Loop over <option> tags
        for (i = 0, o = options.length; i < o; i++) {
            option = options[i];

            // If selected options are less than 1 and country value is NOT 'USA'
            if (selected <= 1 && this.value !== 'USA') {

                // ...check if option is selected and increment "selected" var if true
                if (option.selected) {
                    selected += 1;
                }
            } else {
                // ...otherwise disable the state select ad break out of the loop
                selectState.disabled = true;
                break;
            }
        }
    };
};
window.onload=function(){
var selectCountry=document.getElementById('country'),
selectState=document.getElementById('state');
选择country.onchange=function(){
var options=this.options,
选择=0,i,o;
//循环标记
对于(i=0,o=options.length;i如果选择了(p),为了测试是否选择了多个选项,您需要循环选择选项并测试
selected
属性:

window.onload = function() {
    var selectCountry = document.getElementById('country'),
        selectState = document.getElementById('state');

    selectCountry.onchange = function() {
        var options = this.options,
            selected = 0, i, o;

        // Loop over <option> tags
        for (i = 0, o = options.length; i < o; i++) {
            option = options[i];

            // If selected options are less than 1 and country value is NOT 'USA'
            if (selected <= 1 && this.value !== 'USA') {

                // ...check if option is selected and increment "selected" var if true
                if (option.selected) {
                    selected += 1;
                }
            } else {
                // ...otherwise disable the state select ad break out of the loop
                selectState.disabled = true;
                break;
            }
        }
    };
};
window.onload=function(){
var selectCountry=document.getElementById('country'),
selectState=document.getElementById('state');
选择country.onchange=function(){
var options=this.options,
选择=0,i,o;
//循环标记
对于(i=0,o=options.length;i如果(选择)您的服务器非常支持jQuery,我敢肯定…它除了将其传送到浏览器之外没有什么可做的。web服务器在这方面做得非常好。您的服务器非常支持jQuery,我敢肯定…它除了将其传送到浏览器之外没有什么可做的。web服务器在这方面做得非常好。如果(this.options[I]需要更改此行).selected&&this.options[i].value!=“USA”)非常感谢,这正是我想要的。非常好的声誉分数顺便说一句:如果(this.options[i].selected&&this.options[i].value!=“USA”)需要更改此行,非常感谢,这正是我想要的。非常好的声誉分数顺便说一句:P@sri-很高兴为您提供帮助!快乐编码!:)@sri-很高兴为您提供帮助!快乐编码!:)