0

デフォルト値を使用してチェックボックスを選択する必要があります。参照用に以下のコードを追加しました。私の要件は、サーバー側の値が 1 で、デフォルトのチェックボックスの値も 1 の場合、チェックボックス ID を使用せずに値 1 のチェックボックスを選択する必要があることです。

前もって感謝します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#btnSubmit").click(function(){
        //alert("clciked");
            $(":checkbox").each(function(){
                //alert($(this).val()); 
                if($(this).val()==1){       // assume 1 is a server side value
                $("input[type=checkbox]:contains('1')").attr("checked","checked");
                                /*var checkboxDefaultValue = $("#sampleDiv").find(":checkbox").val();
                                    alert(checkboxDefaultValue + "chkDefVal");
                                    if( checkboxDefaultValue==1){
                                    $("input:checkbox:contains('1')").attr("checked","checked");
                                } */  
                }
            });
        });
    });

</script>
 <body>
 <div id="sampleDiv">
<input type="checkbox" id="chk1" value="1" class="sample" >1
<input type="checkbox" id="chk2" value="2" class = "sample" >2
<input type="checkbox" id="chk3" value="3" class = "sample" >3
</br>
</br>
</div>
<input type="button" id="btnSubmit" value ="Show Values">
 </body>
</html>
4

4 に答える 4

2
//Use $(this) instead of  $("input[type=checkbox]:contains('1')") :
    <script type="text/javascript">
        $(document).ready(function(){
            $("#btnSubmit").click(function(){

                $(":checkbox").each(function(){
                    if($(this).val()==1){     
                    $(this).attr("checked","checked");
                    }
                });
            });
        });

    </script>
于 2013-04-29T04:39:17.637 に答える
1

これを試して:

if($(this).val()==1){
    $(this).prop("checked",true);
}

http://api.jquery.com/propの詳細を読むprop

于 2013-04-29T04:32:18.627 に答える
0

jQuery を使用してそれを行うには、次の 2 つの方法があります。

<script type="text/javascript">
$(document).ready(function () {
    $("#btnSubmit").click(function () {
        //Only use one of the following:
        // this way will only select the first checkbox with the specified value
        $('input:checkbox[value="1"]').first().prop('checked', true);
        // this way will select all checkboxes with the specified value
        $('input:checkbox[value="1"]').prop('checked', true);
    });
});
</script>

おそらくこれが最も効率的な方法だと思います。

于 2013-04-29T04:57:24.940 に答える
0

属性 equals セレクターを使用して、指定された値のチェックボックスを取得できます

$(':checkbox[value="' + 1 + '"]').prop('checked', true)
于 2013-04-29T04:30:06.027 に答える