簡體   English   中英

使用Jquery在頁面加載時有條件地選中此框

[英]Check the box conditionally upon page load using Jquery

頁面加載后,所有復選框均保持未選中狀態(如下圖所示),我從Java會話中獲取了value1文本並將其存儲在JSON對象下,現在我正在比較以下頁面的value1文本是否與value1文本匹配:我從會議開始。 如果找到匹配項,則使復選框為true,否則不要選中它。 我在下面嘗試,但是它選中了所有框。

<script type="text/javascript">
            $.s1.proxy.contentReady(function() {
                var sessionValue = <%= json %>;  //loaded from java session
                sessionValue.forEach(function(sessionValue) {
                         $("input:checkbox[name=myFormProperties]").each(function(){      
                            var val = $(this).closest("tr").find("td:nth-child(2)").text().trim();
                             if(val!=null && val!=''){
                                 if (sessionValue.startsWith(val)) {
                                    alert("session and page value match found ");
                                     $("input:checkbox[name=myFormProperties]").attr('checked', true); // this makes all the checkbox value true. How to make only //specific check box value true here
                                }
                             }
                        });
                    });
            });

        </script> 

選中復選框后,所選值將作為復選框旁邊的鏈接出現,如圖所示。 復選框

那是因為您要在所有名為myFormProperties的復選框上進行設置。

代替$("input:checkbox[name=myFormProperties]").attr('checked', true); (只需再次選擇所有復選框),只需從每個循環中選擇當前復選框即可,如下所示

$(this).attr('checked',true);

您正在用代碼選中所有復選框

$("input:checkbox[name=myFormProperties]").attr('checked', true);    

要選中帶有值的復選框,請在下面使用

$("input[value='" + val + "']").prop('checked', true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM