简体   繁体   English

更新:仅根据单选按钮和所选选项选择某些项目

[英]UPDATED: Only select certain item based on radio button and option selected

I'm new to JQuery and I'm in a bit of a pickle. 我是JQuery的新手,有点不高兴。 I have 3 main radio buttons setup. 我有3个主要的单选按钮设置。 These buttons are to select an item based on the country selected. 这些按钮用于根据所选国家/地区选择项目。

So for example, I have 6 items. 例如,我有6个项目。 3 for domestic (USA) and 3 for international (!=USA). 国内(美国)为3,国际(!= USA)为3。

For example: If I select first_radio, it also selects either something-div-0 or something-div-1 based on option selected. 例如:如果我选择first_radio,它还会根据所选选项选择something-div-0或something-div-1。

JQuery (UPDATED) jQuery(已更新)

<script>
$(document).ready(function() {
$('.something').hide();


$('.somethingName :input').each(function (i, v) {
$(this).attr("id", "something-div-" + i);
i++;
});

$("#option").change(function() {

    var isUS = $("#option:selected").text() === 'UNITED STATES';
    var isNotUS = !isUS;

$(".somethingPick").click(function() {
if(isUS && $("#first_radio").prop('checked') === true){
    $('#something-div-0').click();
} else if(isUS && $("#second_radio").prop('checked') === true){
    $('#something-div-2').click();
} else if(isUS && $("#third_radio").prop('checked') === true){
    $('#something-div-4').click();
} else if(isNotUS && $("#first_radio").prop('checked') === true){
    $('#something-div-1').click(); 
} else if(isNotUS && $("#second_radio").prop('checked') === true){
    $('#something-div-3').click(); 
} else if(isNotUS && $("#third_radio").prop('checked') === true){
    $('#something-div-5').click();
});
});
}); 
</script>

Can you tell me what I'm doing wrong? 你能告诉我我做错了吗?

UPDATE 更新

I updated the JQuery in my jsfiddle. 我在jsfiddle中更新了JQuery。 I'm almost there. 我快到了。 I can get 3 of them working. 我可以让他们三个工作。 I just need help with the other 3. 我只需要其他3。

Here''s my jsfiddle too: https://jsfiddle.net/mjreno/r99qom5v/31/ 这也是我的jsfiddle: https ://jsfiddle.net/mjreno/r99qom5v/31/

Basically your code works, but I see an error while loading jQuery 1.6.4: 基本上,您的代码可以工作,但是在加载jQuery 1.6.4时出现错误:

Blocked loading mixed active content into console 阻止将混合的活动内容加载到控制台

Sincerely I don't know why it gave this error, so I changed jQuery version to last and it loads without problems. 真诚的我不知道为什么会出现此错误,因此我将jQuery版本更改为last,并且加载没有问题。

However I slightly edited your code to be cleaner 但是我稍微修改了您的代码以使其更简洁

$(document).ready(function() {

var isUS = $("#option").val() === 'UNITED STATES';
var isNotUS = !isUS;

$("#option").change(function() {
    isUS = $("#option").val() === 'UNITED STATES';
    isNotUS = !isUS;
    checkRadio();
});

$(".somethingPick").click(function() {
    checkRadio();
});

function checkRadio() {
    $('.something input[type=radio]').removeAttr('checked');

    if(isUS && $("#first_radio").prop('checked')){
        $('#US_1').click();
    } else if(isUS && $("#second_radio").prop('checked')){
        $('#US_2').click();
    } else if(isUS && $("#third_radio").prop('checked')){
        $('#US_3').click();
    } else if(isNotUS && $("#first_radio").prop('checked')){
        $('#NOTUS_1').click();
    } else if(isNotUS && $("#second_radio").prop('checked')){
        $('#NOTUS_2').click();
    } else if(isNotUS && $("#third_radio").prop('checked')){
        $('#NOTUS_3').click();
    }
}

});

Here is the JSFiddle . 这是JSFiddle

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

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