自动提交单选按钮但保持选中状态

时间:2011-09-08 16:55:31

标签: javascript html forms radio-button

我正在使用单选按钮对分页结果进行排序。但是,只要单击按钮并自动提交,它就会被取消选中。我想保持选中按钮,以便用户知道他们选择了哪一个。我怎么能这样做?

这就是我所拥有的:

function autoSubmit() {
    var formObject = document.forms['theForm'];
    formObject.submit();
}

<input type="radio" name="sort" value="time" onChange="autoSubmit();" />
<input type="radio" name="sort" value="year" onChange="autoSubmit();" />
<input type="radio" name="sort" value="name" onChange="autoSubmit();" />

if(isset($_GET["sort"])) { 
    $sort = $_GET["sort"];
}

2 个答案:

答案 0 :(得分:2)

我认为你的代码是这样的:

<?php
$sort = "";
if(isset($_GET["sort"]))
{ $sort = $_GET["sort"]; }
?>
<html>
<head>
<script>
function autoSubmit()
{
    var formObject = document.forms['theForm'];
    formObject.submit();
}
</script>
</head>
<body>
<form name='theForm' id='theForm'>
    <input type="radio" name="sort" <?php if ($sort == 'upload_time') { ?>checked='checked' <?php } ?>value="upload_time" onChange="autoSubmit();" />Recently Uploaded
    <input type="radio" name="sort" <?php if ($sort == 'article') { ?>checked='checked' <?php } ?> value="article" onChange="autoSubmit();" /> Alphabetically
    <input type="radio" name="sort" <?php if ($sort == 'year') { ?>checked='checked' <?php } ?> value="year" onChange="autoSubmit();" /> Most Recent
</form>
</body>
</html>

答案 1 :(得分:1)

这是服务器端。在所选的单选按钮中设置属性checked="checked"