重新加载页面后,选中复选框

时间:2016-03-18 09:15:05

标签: javascript jquery

我需要在页面重新加载后选中复选框我正在尝试此代码 这是我的复选框

<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>

4 个答案:

答案 0 :(得分: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需要“/”在关闭标记

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

答案 1 :(得分:0)

在html输入标记中添加已检查属性=已选中:

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

答案 2 :(得分:0)

看起来您正在使用其他一些基本语言。我使用php,它通过POST,GET和SESSION在全局和你需要的时间存储值。

更好地找到您所用语言的等效功能。值得长期和项目扩展。

答案 3 :(得分: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();
相关问题