如何在回发时保留禁用的复选框? [英] How do I retain disabled checkboxes upon postback?

查看:64
本文介绍了如何在回发时保留禁用的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨成员,



我有这组javascript代码,允许我根据我的选择禁用/启用复选框。



下面的代码允许我将默认值设置为全选,并在第一页加载期间选中全选但不在回发时禁用其余部分。 在回发中不这样做的原因是因为例如如果用户没有选中checkboxlist2中的任何复选框,我想提示用户选中复选框列表2中的至少一个复选框,但不想覆盖之前在回发上的用户选择,使用checkboxlist1和checkboxlist2中的默认全选。



 $( function (){
if ($( #hidden)。val()==
{
$ ( #Checkboxlist1:checkbox)。attr(' disabled'' disabled');
$( #Checkboxlist1:checkbox [value ='Select All'])。removeAttr(' disabled');
$( #Checkboxlist1:checkbox [value ='Select All'])。prop( 选中 true );
$( #Checkboxlist2:checkbox)。attr(' disabled'' 禁用');
$( #Checkboxlist2:checkbox [value ='Select All'])。 removeAttr(' disabled');
$( #Checkboxlist2:checkbox [value ='Select All'])。 prop( 选中 true ) ;
$( #hidden)。val( set);
}
});





以下代码允许我启用/禁用checkboxlist1和checkboxlist2的链接选择。



 $( function (){
$ ( #Checkboxlist1:checkbox)。change( function (){
var ischecked = $( this )。is(< span class =code-string> :checked);
var val = $( this )。val();
// alert(val);
if (val == 全选){
if (缺血){
$( #Checkboxlist1:checkbox)。attr( ' disabled'' 禁用');
$( this )。removeAttr(' 禁用');
$( #Checkboxlist1:checkbox)。prop( 选中 false );
$( this )。prop( 选中 true );
$( #Checkboxlist2:checkbox)。removeAttr(' disabled');

return ;
} else {
$( #Checkboxlist1:复选框)。removeAttr(' disabled');
$( #Checkboxlist2:checkbox)。attr(' disabled'' 禁用');
return ;
}
} else if (val!= 全选){
if (缺席) {
$( #Checkboxlist1:checkbox [value ='Select All']) .prop( 选中 false );
}
} else if (val == 食物){
if (缺血){
$( #Checkboxlist2:checkbox [value ='Chicken'])。removeAttr (' disabled');
$( #Checkboxlist2:checkbox [value = spinach])。removeAttr(< span class =code-string>'
disabled');

} else {
$( #Checkboxlist2:checkbox [value ='Chicken'])。attr( disabled disabled);
$( #Checkboxlist2:checkbox [value ='Spinach'])。attr ( disabled 禁用);


}
} 其他 如果(val == 饮料){
if (缺血){
$( #Checkboxlist2:checkbox [value ='Coke'] )。removeAttr(' disabled');
$( #Checkboxlist2:checkbox [value ='Juice'])。removeAttr (' disabled');

} else {
$( #Checkboxlist2:checkbox [value ='Coke'])。attr( disabled disabled);
$( #Checkboxlist2:checkbox [value = Juice])。attr(< span class =code-string>
disabled 禁用);
}
}
});
});


$( function (){
$( #Checkboxlist2:复选框)。change( function (){
var ischecked = $( this )。is( :选中);
var val = $( this )。val();
// alert(val);
if (val == 全选){
if (缺血){
$( #Checkboxlist2:复选框)。attr(' disabled'' disabled' );
$( this )。removeAttr(' 禁用');
$( #Checkboxlist2:checkbox)。prop( 选中 false );
$( this )。prop( 选中 true );

return ;
} else {
$( #Checkboxlist2:复选框)。removeAttr(' disabled');
return ;
}
}
});
});





我面临的问题是,当我点击提交按钮时,它不会禁用自动读取之前检查的值并在回发期间启用或禁用代码中所需的其他复选框(因为该函数需要.change只有在点击某些内容时才会调用该函数)。



因此,假设用户之前选择了全选,但在回发期间仍然会选中全选,但其他列表项已启用(据说被禁用)



我的问题是:

在回发时我的检查值保留但是之前禁用的复选框已启用,那么如何在回发时保留其整个状态?




我启用了我的复选框列表enableviewstate = true

解决方案

功能(){
if


< blockquote>( #hidden)。val()==
{


#Checkboxlist1:复选框)。attr(' disabled'' disabled');


Hi members,

I have this set of javascript codes which allows me to disable/enable checkboxes according to my selection.

The codes below allows me to set the default as "Select All" and disables the rest when "Select All" is checked during the 1st page load but not on postback. The reason to not do it in post back is because e.g If a user did not check any checkbox in checkboxlist2, I want to prompt the user the select the check at least one checkbox in checkboxlist 2 but do not want to overwrite the user selection previously on postback with the default "Select All" in checkboxlist1 and checkboxlist2.

$(function () {
    if($("#hidden").val() == "")
    {
        $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1 :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", true);
        $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist2 :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist2 :checkbox[value='Select All']").prop("checked", true);
        $("#hidden").val("set");
    }
});



The codes below allows me enable/disable the link selections of checkboxlist1 and checkboxlist2.

$(function () {
    $("#Checkboxlist1 :checkbox").change(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {
                    $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $("#Checkboxlist1  :checkbox").prop("checked", false);
                    $(this).prop("checked", true);
                    $("#Checkboxlist2 :checkbox").removeAttr('disabled');
 
                    return;
                } else {
                    $("#Checkboxlist1 :checkbox").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
                    return;
                }
            } else if (val != "Select All") {
                if (ischecked) {
                    $("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", false);
                }
            } else if (val == "Food") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox[value='Chicken']").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox[value=spinach]").removeAttr('disabled');
 
                } else {
                    $("#Checkboxlist2 :checkbox[value='Chicken']").attr("disabled", "disabled");
                  $("#Checkboxlist2 :checkbox[value='Spinach']").attr("disabled", "disabled");
 

                }
            } else if (val == "Drinks") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox[value='Coke']").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox[value='Juice']").removeAttr('disabled');
 
                } else {
                    $("#Checkboxlist2 :checkbox[value='Coke']").attr("disabled", "disabled");
                    $("#Checkboxlist2 :checkbox[value=Juice]").attr("disabled", "disabled");
                }
            }
        });
    });
 

$(function () {
    $("#Checkboxlist2 :checkbox").change(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $("#Checkboxlist2  :checkbox").prop("checked", false);
                    $(this).prop("checked", true);
 
                    return;
                } else {
                    $("#Checkboxlist2  :checkbox").removeAttr('disabled');
                    return;
                }
            }
        });
    });



My problem I am facing is that when I click on submit button, it does not disable automatically read the previous checked values and enable or disable other checkboxes as required in the codes during postback (since the function requires .change which only when something is click the function will only then called).

So lets say the user previously chose "Select All" but during post back the "Select All' is still checked but the rest of the list items are enabled (Supposedly to be disabled)

My question is:
upon postback my check values retains but the checkboxes disabled previously are enabled so how do I retain its whole state upon postback?


I did enabled my checkboxlist enableviewstate = true

解决方案

(function () { if(


("#hidden").val() == "") {


("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');


这篇关于如何在回发时保留禁用的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆