简体   繁体   English

复选框提醒脚本在动态加载包含复选框的test.php时不记得复选框状态

[英]Checkbox reminder script doesn't remember checkbox status when dynamically load test.php which contains the checkbox

I have a working checkbox reminder script: http://mauricederegt.nl/test/index.html 我有一个工作复选框提醒脚本: http//mauricederegt.nl/test/index.html

It simply remembers the checkbox status, so when you come back to the webpage later, it will still be checked to unchecked. 它只是记住复选框状态,因此当您稍后返回网页时,仍会检查它是否取消选中。 This works all great. 这一切都很棒。

Now I need to dynamically load a php page in my HTML page (the original php page gets some stuff out of my database and displays a list of names. Underneath that list, this checkbox appears, followed by another list from the db). 现在我需要在我的HTML页面中动态加载一个php页面(原始的php页面从我的数据库中获取一些东西并显示一个名称列表。在该列表下面,出现此复选框,然后是来自db的另一个列表)。

I've placed the checkbox in the php file, but it will be shown in the HTML file when the php file is loaded. 我已经将复选框放在php文件中,但是当加载php文件时它将显示在HTML文件中。 The js file is still loaded in the HTML file (also tried loading it in the php file, but this had no effect). js文件仍然加载在HTML文件中(也尝试将其加载到php文件中,但这没有任何效果)。

PROBLEM: The checkbox isn't remembered anymore :( See this demo: http://mauricederegt.nl/test/index2.html 问题:不再记住该复选框:(见此演示: http//mauricederegt.nl/test/index2.html

I think this is because the page is dynamically loaded now and isn't visible in the HTML? 我认为这是因为页面现在是动态加载的并且在HTML中不可见?

How can I fix this, so the checkbox is remembered again? 我该如何修复此问题,以便再次记住该复选框?

Please see the source code of the HTML files for the js code if needed (is too much to post here) 如果需要,请查看js代码的HTML文件的源代码(这里发布的内容太多了)

Kind regards, 亲切的问候,

the php code: php代码:

   <?php  
$q=$_GET["q"];
$checked = ($_GET['checked'] == 1 ) ? 'checked="checked"' : '';

if ($q == 1) {
echo '<ul>
        <li>Mike</li>
        <li>Peter</li>
        <li>Richard</li>
        <li>Quintin</li>
        </ul>
<div id="soundcheck" class="button blue"><input type="checkbox" value="Name" id="sound" '.$checked.' /><label for="sound"></label></div>
<ul>
        <li>Apple</li>
        <li>Banana</li>
        <li>Pineapple</li>
        </ul>';
}
?>

The problem is that the code to check and then set the remembered status for the checkboxes is only called when the page is first loaded (bound to the DOM ready event), and therefore isn't called when you dynamically load content. 问题是,检查然后设置复选框的记忆状态的代码仅在首次加载页面时被调用(绑定到DOM ready事件),因此在动态加载内容时不会调用。

Call this part of your existing code: 将此部分内容称为现有代码:

$('div#soundcheck input:checkbox').each(function() {
    // on load, set the value to what we read from storage:
    var val = storedData.get(this.id);
    if (val == 'checked') $(this).attr('checked', 'checked');
    if (val == 'not') $(this).removeAttr('checked');
    if (val) $(this).trigger('change');
});

after dynamically loading your checkboxes, and that should fix the problem. 动态加载复选框后,应该解决问题。

Edit: You'll need to add the above code to this section of your existing Javascript: 编辑:您需要将以上代码添加到现有Javascript的此部分:

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("scoreboard").innerHTML=xmlhttp.responseText;
        // add the above code here
    }
}

If the php that loads the GET, I assume it returns the following: 如果加载GET的php,我假设它返回以下内容:

<php
echo <<<OUTPUT
<div id="soundcheck" class="button blue">
<input type="checkbox" value="Name" id="sound">
This is the checkbox, uncheck it and refresh the page. It should remain unchecked!
</div>
OUTPUT;

Instead, have it return: 相反,让它返回:

To make it a bit tighter, I would would go with: 为了使它更紧凑,我会选择:

<php
echo <<<OUTPUT
<div id="soundcheck" class="button blue">
<input type="checkbox" value="Name" id="sound">
<label for="sound">This is the checkbox, uncheck it and refresh the page. It should remain unchecked!</label>
</div>
OUTPUT;

This will always return a blank checkbox, regardless of what they submitted. 这将始终返回一个空白复选框,无论他们提交什么。

Now if the issue is that it should stay checked unless they uncheck the box, go with: 现在,如果问题是它应该保持检查,除非他们取消选中该框,请执行以下操作:

<php
$checked ($_GET['checked'] == 1 ) ? 'checked="checked" : '';
echo <<<OUTPUT
<div id="soundcheck" class="button blue">
<input type="checkbox" value="Name" id="sound" $checked>
<label for="sound">This is the checkbox, uncheck it and refresh the page. It should remain unchecked!</label>
</div>
OUTPUT;

With the following in your js: 在你的js中有以下内容:

xmlhttp.open("GET","test.php?q="+str+"&checked=1",true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM