如果选择了选项A,则显示Div X,如果选择了所有其他选项,则使用Jquery显示Div Y.

时间:2016-04-12 15:10:16

标签: javascript jquery html

我不知道这是否可行,因为我从未需要这样做但我需要根据是否选择国家来显示或隐藏美国号码输入字段或国际号码输入字段。我不想特别打电话给每一个val =""因为这似乎是一种糟糕的做法,而是使用其他如果选择除值以外的任何东西=" US" 比如

如果val =" US"显示美国数字字段别隐藏

如果val ="其他一切"显示国际号码字段隐藏

我创造了一个工作小提琴来澄清我的意思。任何帮助将不胜感激。我似乎无法找到解决这个问题的方法

https://jsfiddle.net/gappleton/k5c0xha0/3/

<html>
<div class="input-icon country">
<span class="fa fa-globe"></span> 
<select name="country" id="country">
    <option value="">Country</option>
    <option value="USA">United States</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    <option value="AD">Andorra</option>
    <option value="AO">Angola</option>
    <option value="AI">Anguilla</option>
    <option value="AG">Antigua and Barbuda</option>
    <option value="AR">Argentina</option>
    <option value="AM">Armenia</option>
    <option value="AW">Aruba</option>
    <option value="AU">Australia</option>
    <option value="AT">Austria</option>
    <option value="AZ">Azerbaijan Republic</option>
    <option value="BS">Bahamas</option>
    <option value="BH">Bahrain</option>
    <option value="BB">Barbados</option>
    <option value="BE">Belgium</option>
    <option value="BZ">Belize</option>
    <option value="BJ">Benin</option>
    <option value="BM">Bermuda</option>
    <option value="BT">Bhutan</option>
    <option value="BO">Bolivia</option>
    <option value="BA">Bosnia and Herzegovina</option>
    <option value="BW">Botswana</option>
    <option value="BR">Brazil</option>
    <option value="BN">Brunei</option>
    <option value="BG">Bulgaria</option>
    <option value="BF">Burkina Faso</option>
    <option value="BI">Burundi</option>
    <option value="KH">Cambodia</option>
    <option value="CA">Canada</option>
    <option value="CV">Cape Verde</option>
    <option value="KY">Cayman Islands</option>
    <option value="TD">Chad</option>
    <option value="CL">Chile</option>
    <option value="C2">China Worldwide</option>
    <option value="CO">Colombia</option>
    <option value="KM">Comoros</option>
    <option value="CK">Cook Islands</option>
    <option value="CR">Costa Rica</option>
    <option value="HR">Croatia</option>
    <option value="CY">Cyprus</option>
    <option value="CZ">Czech Republic</option>
    <option value="CD">Democratic Republic of the Congo</option>
    <option value="DK">Denmark</option>
    <option value="DJ">Djibouti</option>
    <option value="DM">Dominica</option>
    <option value="DO">Dominican Republic</option>
    <option value="EC">Ecuador</option>
    <option value="EG">Egypt</option>
    <option value="SV">El Salvador</option>
    <option value="ER">Eritrea</option>
    <option value="EE">Estonia</option>
    <option value="ET">Ethiopia</option>
    <option value="FK">Falkland Islands</option>
    <option value="FO">Faroe Islands</option>
    <option value="FJ">Fiji</option>
    <option value="FI">Finland</option>
    <option value="FR">France</option>
    <option value="GF">French Guiana</option>
    <option value="PF">French Polynesia</option>
    <option value="GA">Gabon Republic</option>
    <option value="GM">Gambia</option>
    <option value="GE">Georgia</option>
    <option value="DE">Germany</option>
    <option value="GI">Gibraltar</option>
    <option value="GR">Greece</option>
    <option value="GL">Greenland</option>
    <option value="GD">Grenada</option>
    <option value="GP">Guadeloupe</option>
    <option value="GT">Guatemala</option>
    <option value="GN">Guinea</option>
    <option value="GW">Guinea Bissau</option>
    <option value="GY">Guyana</option>
    <option value="HN">Honduras</option>
    <option value="HK">Hong Kong</option>
    <option value="HU">Hungary</option>
    <option value="IS">Iceland</option>
    <option value="IN">India</option>
    <option value="ID">Indonesia</option>
    <option value="IE">Ireland</option>
    <option value="IL">Israel</option>
    <option value="IT">Italy</option>
    <option value="JM">Jamaica</option>
    <option value="JP">Japan</option>
    <option value="JO">Jordan</option>
    <option value="KZ">Kazakhstan</option>
    <option value="KE">Kenya</option>
    <option value="KI">Kiribati</option>
    <option value="KW">Kuwait</option>
    <option value="KG">Kyrgyzstan</option>
    <option value="LA">Laos</option>
    <option value="LV">Latvia</option>
    <option value="LS">Lesotho</option>
    <option value="LI">Liechtenstein</option>
    <option value="LT">Lithuania</option>
    <option value="LU">Luxembourg</option>
    <option value="MG">Madagascar</option>
    <option value="MW">Malawi</option>
    <option value="MY">Malaysia</option>
    <option value="MV">Maldives</option>
    <option value="ML">Mali</option>
    <option value="MT">Malta</option>
    <option value="MH">Marshall Islands</option>
    <option value="MQ">Martinique</option>
    <option value="MR">Mauritania</option>
    <option value="MU">Mauritius</option>
    <option value="YT">Mayotte</option>
    <option value="MX">Mexico</option>
    <option value="FM">Micronesia</option>
    <option value="MN">Mongolia</option>
    <option value="MS">Montserrat</option>
    <option value="MA">Morocco</option>
    <option value="MZ">Mozambique</option>
    <option value="NA">Namibia</option>
    <option value="NR">Nauru</option>
    <option value="NP">Nepal</option>
    <option value="NL">Netherlands</option>
    <option value="AN">Netherlands Antilles</option>
    <option value="NC">New Caledonia</option>
    <option value="NZ">New Zealand</option>
    <option value="NI">Nicaragua</option>
    <option value="NE">Niger</option>
    <option value="NU">Niue</option>
    <option value="NF">Norfolk Island</option>
    <option value="NO">Norway</option>
    <option value="OM">Oman</option>
    <option value="PW">Palau</option>
    <option value="PA">Panama</option>
    <option value="PG">Papua New Guinea</option>
    <option value="PE">Peru</option>
    <option value="PH">Philippines</option>
    <option value="PN">Pitcairn Islands</option>
    <option value="PL">Poland</option>
    <option value="PT">Portugal</option>
    <option value="QA">Qatar</option>
    <option value="CG">Republic of the Congo</option>
    <option value="RE">Reunion</option>
    <option value="RO">Romania</option>
    <option value="RU">Russia</option>
    <option value="RW">Rwanda</option>
    <option value="KN">Saint Kitts and Nevis Anguilla</option>
    <option value="PM">Saint Pierre and Miquelon</option>
    <option value="VC">Saint Vincent and Grenadines</option>
    <option value="WS">Samoa</option>
    <option value="SM">San Marino</option>
    <option value="ST">São Tomé and Príncipe</option>
    <option value="SA">Saudi Arabia</option>
    <option value="SN">Senegal</option>
    <option value="RS">Serbia</option>
    <option value="SC">Seychelles</option>
    <option value="SL">Sierra Leone</option>
    <option value="SG">Singapore</option>
    <option value="SK">Slovakia</option>
    <option value="SI">Slovenia</option>
    <option value="SB">Solomon Islands</option>
    <option value="SO">Somalia</option>
    <option value="ZA">South Africa</option>
    <option value="KR">South Korea</option>
    <option value="ES">Spain</option>
    <option value="LK">Sri Lanka</option>
    <option value="SH">St. Helena</option>
    <option value="LC">St. Lucia</option>
    <option value="SR">Suriname</option>
    <option value="SJ">Svalbard and Jan Mayen Islands</option>
    <option value="SZ">Swaziland</option>
    <option value="SE">Sweden</option>
    <option value="CH">Switzerland</option>
    <option value="TW">Taiwan</option>
    <option value="TJ">Tajikistan</option>
    <option value="TZ">Tanzania</option>
    <option value="TH">Thailand</option>
    <option value="TG">Togo</option>
    <option value="TO">Tonga</option>
    <option value="TT">Trinidad and Tobago</option>
    <option value="TN">Tunisia</option>
    <option value="TR">Turkey</option>
    <option value="TM">Turkmenistan</option>
    <option value="TC">Turks and Caicos Islands</option>
    <option value="TV">Tuvalu</option>
    <option value="UG">Uganda</option>
    <option value="UA">Ukraine</option>
    <option value="AE">United Arab Emirates</option>
    <option value="GB">United Kingdom</option>
    <option value="UY">Uruguay</option>
    <option value="VU">Vanuatu</option>
    <option value="VA">Vatican City State</option>
    <option value="VE">Venezuela</option>
    <option value="VN">Vietnam</option>
    <option value="VG">Virgin Islands (British)</option>
    <option value="WF">Wallis and Futuna Islands</option>
    <option value="YE">Yemen</option>
    <option value="ZM">Zambia</option>
</select>
</div>

<!-- US Number div-->
<div id="us-number" class="input-icon inline">
    <span class="fa fa-phone-square"></span>
    <input type="tel" value="" name="phone" placeholder="Phone Number*" title="Format: xxx-xxx-xxxx" pattern="^\d{3}-\d{3}-\d{4}$" required />
</div>
<!-- US Number div Hide-->

<!-- US Number div-->
<div id="int-number" class="input-icon inline">
    <span class="fa fa-phone-square"></span>
    <input type="tel" value="" name="phone" placeholder=" Int Phone Number*" title="Format: xxx-xxx-xxxx" pattern="^\d{3}-\d{3}-\d{4}$" required />
</div>
<!-- US Number div Hide-->
</html>

<script>
$(document).ready(function(){

$('#us-number').hide();
$('#country').change(function () {
    if ($('#country').val() == "USA") {
        $('#us-number').show();
    } else {
        $('#us-number').hide();
    }
});

$('#int-number').hide();
$('#country').change(function () {
    if ($('#country').val() == "AL") {
        $('#int-number').show();
    } else {
        $('#int-number').hide();
    }
});

});

</script>

2 个答案:

答案 0 :(得分:0)

您可以在单个change事件函数

中处理此方案
$(document).ready(function(){
// jquery multiple selector. Dont need to seperately write hide for us-number & int-number
$('#us-number, #int-number').hide(); 
$('#country').change(function () {
    if ($('#country').val() == "USA") {
    $('#int-number').hide(); // hide international num field
        $('#us-number').show(); //show us num field
    } else {
    $('#int-number').show(); // show international num field
        $('#us-number').hide(); // hide us num field
    }
});
});

Working Example

修改

正如Koen所指出,用户仍然可以从下拉列表中选择Country选项。在那种情况下,所有输入都将进行。因此添加了else-if循环以检查值是否为空字符串然后隐藏输入字段

$(document).ready(function(){
$('#us-number, #int-number').hide();
$('#country').change(function () {

    if ($('#country').val() == "USA") {
    $('#int-number').hide();
        $('#us-number').show();
    } else if($('#country').val() == ""){ // Added this else if
      $('#us-number, #int-number').hide();
    }
    else {
    $('#int-number').show();
        $('#us-number').hide();
    }
});
});

Edited fiddle

答案 1 :(得分:0)

我刚才为此目的编写了一个小型库,名为Visiblary

/*
 * params:
 *     - selector: jQuery selector
 *     - init: function which gets the Visiblary object to initialize it
 *     - events: object/array
 *         - root: a selector which contain all the affected tags, default is "body"
 *         - types: array, which contains the string representation of the events
 *         - selector: inner selector to identify the target set
 *         - handler: handler function
 */
function Visiblary(params) {
    var instance = this;
    if (!params.toggleClass) {
        params.toggleClass = "invisible";
    }
    this.hideAll = function() {
        $(params.selector).addClass(params.toggleClass);
        return instance;
    };
    this.hideSubset = function(subsetSelector) {
        $(params.selector).filter(subsetSelector).addClass(params.toggleClass);
        return instance;
    };
    this.hideOnly = function(subsetSelector) {
        $(params.selector).not(subsetSelector).removeClass(params.toggleClass);
        $(params.selector).filter(subsetSelector).addClass(params.toggleClass);
        return instance;
    };
    this.showAll = function() {
        $(params.selector).removeClass(params.toggleClass);
        return instance;
    };
    this.showSubset = function(subsetSelector) {
        $(params.selector).filter(subsetSelector).removeClass(params.toggleClass);
        return instance;
    };
    this.showOnly = function(subsetSelector) {
        $(params.selector).not(subsetSelector).addClass(params.toggleClass);
        $(params.selector).filter(subsetSelector).removeClass(params.toggleClass);
        return instance;
    };
    this.invert = function() {
        $(params.selector).each(function() {
            $(this).hasClass(params.toggleClass) ? $(this).removeClass(params.toggleClass) : $(this).addClass(params.toggleClass);
        });
        return instance;
    };
    if (!!params.init) {
        params.init(this);
    }
    if (!!params.events) {
        for (var event in params.events) {
            $(!!params.events[event].root ? params.events[event].root : "body").on(params.events[event].types.join(" "), params.events[event].selector, params.events[event].handler);
        }
    }
    return instance;
}

示例:

var actionButtonVisiblary = new Visiblary({
    selector: ".admin-page",
    events: [
        {
            root: "#admin-action-button-container > .button-action",
            types: ["click"],
            handler: function(e) {
                actionButtonVisiblary.showOnly($(this).data("what"));
            }
        }
    ]
});