Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jquery中选中复选框时自动选择单选按钮_Jquery_Radio Button - Fatal编程技术网

在jquery中选中复选框时自动选择单选按钮

在jquery中选中复选框时自动选择单选按钮,jquery,radio-button,Jquery,Radio Button,我有一个3个单选按钮的列表,其中第一个单选按钮是在加载时选择的。第三个按钮有一组与其相关的复选框。 选中其中一个复选框时,应自动选择相关单选按钮。问题是,这个动作在Safari的Chrome中不起作用,但在FF、Opera甚至IE上都很好(如果IE可以做到的话…)。我使用的是ajax,所以当页面刷新时,它会返回到被选中的第一个收音机,这会取消操作,这只发生在Chrome和Safari中 发生什么事了?请有人帮帮我 代码如下: jquery+Ajax: $.ajax({ type: "POST",

我有一个3个单选按钮的列表,其中第一个单选按钮是在加载时选择的。第三个按钮有一组与其相关的复选框。 选中其中一个复选框时,应自动选择相关单选按钮。问题是,这个动作在Safari的Chrome中不起作用,但在FF、Opera甚至IE上都很好(如果IE可以做到的话…)。我使用的是ajax,所以当页面刷新时,它会返回到被选中的第一个收音机,这会取消操作,这只发生在Chrome和Safari中

发生什么事了?请有人帮帮我

代码如下:

jquery+Ajax:

$.ajax({
type: "POST",
dataType: "text",
url: "ajax/possibleValues.html",
data: $("form#orderDefinition").serialize(),
success: function(response){
    $('#usercontent .sleeve .toprow').html(response);
    //alert(response);
    applyValidation();
    radioButtonHighlightSelection();
},
error: function(response, ioArgs, err) {
    if (response.status == 601) {
        sessionTimedOut();
    }
}         
});

$("#chooseSource input:radio").each(function(e){
    if($(this).is(":checked")){
        $(this).parent().addClass("selected");
    }
});

$("#chooseSource input:checkbox").each(function(){
    if($(this).is(":checked")){
        $(this).parent().parent().prev().find("input:radio").attr("checked", true);
        $(this).parent().parent().prev().find("input:radio").parent().addClass("selected");
        $(this).parent().parent().addClass("selected");
    }
});
Html:


网站
杂志
朋友
其他
其他项目的复选框项目1
其他项目的复选框项目2
其他项目的复选框项目3
谢谢

太长,无法完全发布,但这应该适用于所有浏览器。这是JS,我为你修正了一些东西:

$("#chooseSource input:radio").change(function(e){
    $("#chooseSource input:radio").parent().removeClass("selected")
    $(this).parent().addClass("selected");

});

$("#chooseSource input:checkbox").change(function(){
   //toggle class for this select box
   $(this).parent().toggleClass("selected");

   if($(this).is(":checked")){
    //remove all classes from radio boxes
     $("#chooseSource input:radio").parent().removeClass("selected")
     $(this).parent().parent().prev().find("input:radio").attr("checked", true);
     $(this).parent().parent().prev().find("input:radio").parent().addClass("selected");

   }
});​

没有人可以回答你的问题而不看代码。代码已上载。我没有写代码,因为每当我写的时候,没有人敢回答,因为问题很长。@Litso:Lol。。。我以为它很长。。。哇,你的代码看起来可以用了,所以我现在就试试,给你反馈。嗯,所以这与“更改”事件有关。是的,你的代码只在网站加载时运行,然后就再也没有运行过。使用
.change()
时,每次其中一个收音机/复选框更改时,代码都会激活:)(就像html中的
onchange=“…”
)。当我说long时,我指的是html,但我只改变了html中的一件事(第一个收音机上的默认值
class=“selected”
),所以忽略这句话:P@Litso:它可以工作,但因为我也在使用ajax,当浏览器刷新时,只会进行选择,但CSS类不会保留。它出现了,然后又消失了。但我想让它留下来。也许我错过了什么。此外,选中复选框后,相关收音机也会被选中,然后取消选中。与浏览器刷新有关。嗯,也许你应该将类切换放在一个单独的函数中,并将其绑定到change事件和pageload事件。不,它仍然不起作用。我甚至试过live(),但没有好结果。
$("#chooseSource input:radio").change(function(e){
    $("#chooseSource input:radio").parent().removeClass("selected")
    $(this).parent().addClass("selected");

});

$("#chooseSource input:checkbox").change(function(){
   //toggle class for this select box
   $(this).parent().toggleClass("selected");

   if($(this).is(":checked")){
    //remove all classes from radio boxes
     $("#chooseSource input:radio").parent().removeClass("selected")
     $(this).parent().parent().prev().find("input:radio").attr("checked", true);
     $(this).parent().parent().prev().find("input:radio").parent().addClass("selected");

   }
});​