[英]Keep the selected color by clicking even after reload the page using Javascript
[英]selected checkbox even after reload the page
重新加載頁面后,我需要選中復選框,我正在嘗試此代碼。
這是我的復選框
<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>
為什么不只使用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" />
在您的html輸入標簽中添加屬性checked = checked:
<input checked="checked" type="checkbox" class="bodyBlack" id="check" name="all" value='all' onClick="checkBox12()" style="margin-left:-691px"/>
看起來您正在使用其他一些基本語言。 我使用php,它覆蓋POST,GET和SESSION來全局存儲值,並在您需要的時間存儲。
最好用您的語言找到一個等效的函數。 從長期和項目擴展上都值得。
您可以將復選框的狀態存儲在Cookie上,並在頁面重新加載后重新填充它。
這里也有一個例子!
$(":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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.