2016-03-18 60 views
0

我需要在頁面重新加載後選中複選框我正在嘗試此代碼。
這裏我複選框即使重新加載頁面後選中複選框

<tr> 
    <td class="label" style="text-align:right">Company:</td> 
    <td class="bodyBlack"> 
    <%=c B.getCompanyName() %> 
    </td> 
    <td> 
    <input type="checkbox" class="bodyBlack" id="check" name="all" value='all' onClick="checkBox12()" style="margin-left:-691px">> Show all paid and unpaid transactions 
    <br> 
    </td> 
</tr> 
//here java script code 
<script type="text/javascript"> 
    function checkBox12() { 
    var jagdi = document.getElementById("check").value; 
    if (jagdi != "") { 
     document.getElementById("check").checked = true; 
    } 
    console.log("jagdi is " + jagdi); 
    //here my url 
    window.location.replace("/reports/buyers/statementAccount.jsp?all=" + jagdi); 
    return $('#check').is(':checked'); 
    } 
</script> 

回答

0

你爲什麼不使用jQuery的唯一?

function checkBox12() 
{ 
    var jagdi = false; 
    if($("#check").length != 0) // use this if you wanted to verify if the element #check is present 
    jagdi = $("#check").prop("checked"); 

//here my url 
window.location.replace("/reports/buyers/statementAccount.jsp?all="+jagdi); 
} 

爲了回答你的問題,你可以檢查你的複選框,當文檔準備好

$(document).ready(function() { $("check").prop("checked", true"); });

但更好的方法是在你的HTML添加checked="checked"。該複選框默認會被選中。 !/ \輸入需要密切標籤 「/」

<input type="checkbox" class="bodyBlack" id="check" name="all" value='all' onClick="checkBox12()" style="margin-left:-691px" checked="checked" /> 
0

添加屬性檢查=在你的HTML input標籤檢查:

<input checked="checked" type="checkbox" class="bodyBlack" id="check" name="all" value='all' onClick="checkBox12()" style="margin-left:-691px"/> 
0

看起來你正在使用一些其他的基本語言。我使用php,並且它將POST,GET和SESSION存儲在全局和您需要的時間值。

更好地找到您的語言中的等效函數。長期價值和項目擴張。

0

您可以在Cookie上存儲複選框狀態,並在頁面重新加載後重新填充。

也有在這裏HERE!

$(":checkbox").on("change", function(){ 
    var checkboxValues = {}; 
    $(":checkbox").each(function(){ 
     checkboxValues[this.id] = this.checked; 
    }); 
    $.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' }) 
    }); 

    function repopulateCheckboxes(){ 
    var checkboxValues = $.cookie('checkboxValues'); 
    if(checkboxValues){ 
     Object.keys(checkboxValues).forEach(function(element) { 
     var checked = checkboxValues[element]; 
     $("#" + element).prop('checked', checked); 
     }); 
    } 
    } 

    $.cookie.json = true; 
    repopulateCheckboxes(); 
爲例
相關問題