2016-03-18 57 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(); 
为例
相关问题