1

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

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

我有这个 javascript 代码

window.onload = function() {
    var selectCountry = document.getElementById('country');
    selectCountry.onchange = function() {
        document.getElementById('state').disabled = (selectCountry.value != 'USA')? true : false;
    };
};

This code disables the select box when countries other than USA is selected. 即使在第一个下拉菜单中选择了多个国家/地区,我也希望它被禁用(如果用户选择 USA 和 CAN,它也应该被禁用)。有什么建议么。我在 Jquery 中有工作代码,但我的服务器似乎根本不支持 Jquery,我希望它在传统 JS 中。

4

3 回答 3

3

只需浏览所有选定的选项并检查其中一个选项的值是否等于 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;
    };

请注意,this将指向selectCountry元素。

此外,服务器从不“支持”jquery。您只需将 jquery.js 文件放在网站目录下并在您的网站上添加指向它的链接。

您可以通过使用selecteOptions属性获得稍微简单的代码,但无法找到它是否被所有浏览器支持:

var disabled = false;
    for(var i = 0; i<this.selectedOptions.length; i++){
        if(this.selectedOptions[i].value == "3") {
            disabled =true;
             break
        }
    }
于 2013-01-22T11:51:13.300 回答
2

这是一些应该对您有所帮助的代码。您必须遍历所有选项元素,检查它们是否被选中。如果是,那么您需要将其与“USA”相匹配 -

鉴于此 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!");
    }
};

这是一个简单的演示

于 2013-01-22T11:50:58.360 回答
2

为了测试是否选择了多个选项,您需要遍历选项并测试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;
            }
        }
    };
};

正如 SebastianG 所说,运行 jQuery 并不依赖于服务器。您可以在任何服务器上运行 jQuery - 您只需要在引用“jQuery”或“$”别名之前确保它从您的服务器或选择的 CDN 正确加载。这是一个入门教程,详细介绍了 jQuery(Tiny URL'd for space): http ://tinyurl.com/arhmcro

于 2013-01-22T12:09:33.673 回答