在xhtml页面上移动时保持复选框的状态

时间:2014-11-13 00:04:23

标签: html checkbox xhtml

我有一个xhtml页面,其中checkbox为其中一个元素。我使用:

创建了checkbox
<td align="right" width="1%"><input type="checkbox" name="check" value="#{checkState.val}" /></td>

现在我想要的是,如果用户检查了这个checkbox然后通过单击某些按钮移动到另一个页面,这些按钮也位于与此复选框相同的html页面上,并且如果用户返回到再次显示此页面,checkbox的状态应保持选中状态。因此,我想保留checkbox的状态。简而言之,我在page1上有checkbox,我检查了这个checkbox,转到第2页,做了一些事情并返回第1页然后checkbox应该被检查。如果最初没有检查checkbox,那么当我返回时不应该检查它。

我对xhtml很新,并且不知道如何做到这一点。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

    //This is the html side where you would make a call to getCheckBoxData.php which lives on the server
    function load()
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState===4 && xmlhttp.status===200)
            {
                document.getElementById("checkbox").innerHTML=xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","getCheckBoxData.php",true);
        xmlhttp.send();
    }

    //this is your php on the back end
    $con=mysqli_connect(connection::url(),connection::user(),connection::pas(),connection::dbName());
    $session=new session();
    $session->startSession();
    $query = "SELECT checkboxfield FROM checkboxtable;
    $result = mysqli_query($con,$query);
    $row = mysqli_fetch_array($result);
    $value= $row['checkboxfield'];
    echo $value;//This sends text back to the client and will be in the response text above.

这是一个使用php并在服务器上设置MySQL数据库的示例。在这种情况下,您的复选框字段可以传回文本“已选中”或“未选中”,您可以在页面加载时放入该值。希望这有帮助