Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 复选框提示脚本没有';动态加载包含复选框的test.php时,请不要记住复选框状态_Javascript_Jquery_Ajax_Checkbox_Dynamic Loading - Fatal编程技术网

Javascript 复选框提示脚本没有';动态加载包含复选框的test.php时,请不要记住复选框状态

Javascript 复选框提示脚本没有';动态加载包含复选框的test.php时,请不要记住复选框状态,javascript,jquery,ajax,checkbox,dynamic-loading,Javascript,Jquery,Ajax,Checkbox,Dynamic Loading,我有一个可用的复选框提示脚本: 它只会记住复选框的状态,因此当您稍后返回网页时,它仍然会被选中为未选中。这非常有效 现在,我需要在我的HTML页面中动态加载一个php页面(原始php页面从我的数据库中获取一些内容,并显示一个名称列表。在该列表下面,将显示此复选框,然后是数据库中的另一个列表) 我已将复选框放在php文件中,但当加载php文件时,它将显示在HTML文件中。js文件仍然加载在HTML文件中(也尝试将其加载到php文件中,但没有效果) 问题:复选框不再被记住:(请参阅此演示: 我认为这

我有一个可用的复选框提示脚本:

它只会记住复选框的状态,因此当您稍后返回网页时,它仍然会被选中为未选中。这非常有效

现在,我需要在我的HTML页面中动态加载一个php页面(原始php页面从我的数据库中获取一些内容,并显示一个名称列表。在该列表下面,将显示此复选框,然后是数据库中的另一个列表)

我已将复选框放在php文件中,但当加载php文件时,它将显示在HTML文件中。js文件仍然加载在HTML文件中(也尝试将其加载到php文件中,但没有效果)

问题:复选框不再被记住:(请参阅此演示:

我认为这是因为页面现在是动态加载的,在HTML中不可见

如何修复此问题,以便再次记住复选框

如果需要js代码,请查看HTML文件的源代码(太多,无法在此发布)

亲切问候,

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>';
}
?>

问题在于,要检查并设置复选框记忆状态的代码仅在首次加载页面(绑定到DOM就绪事件)时调用,因此在动态加载内容时不会调用

将这部分现有代码称为:

$('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');
});
在动态加载复选框之后,这应该可以解决问题

编辑: 您需要将上述代码添加到现有Javascript的此部分:

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

如果加载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;

谢谢,但是我该怎么做呢?而且test.php文件可以在原始文件中调用几次,而不仅仅是在页面加载时。如何处理这个问题?做了一些研究并尝试了
$(document)。ready(function()){
但是运气不好。@Anthony Grist如何在代码方面做到这一点?有什么想法吗?@Maurice请查看编辑。您需要在处理AJAX调用的代码中调用它,发送成功响应。感谢您的回复。做了更改,但没有任何效果,请参阅@Maurice I在Firebug中收到一个Javascript错误,表示
storedData
未定义,这是由于未将其声明为全局变量,而是作为传递给
check.js中
jQuery()
函数的匿名函数的本地变量造成的。请删除
var
关键字,然后重试。更新:this
$checked=($\u GET['checked']==1)?'checked=“checked”:“;
给了我一个语法错误:说:
解析错误:语法错误,意外”;“
@Anthony有什么想法吗?啊,找到了,里面有一个拼写错误(缺少a')。但它仍然不起作用。更新了上面的php文件,请参见
<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;
<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;
xmlhttp.open("GET","test.php?q="+str+"&checked=1",true);