Auto-submit a radio button but keep it selected

血红的双手。 提交于 2019-12-14 04:10:03

问题


I am using radio buttons to sort through paginated results. However, whenever the button is clicked and auto-submitted, it becomes unselected. I want to keep the button selected so that the user knows which one they selected. How can I do that?

Here is what I have:

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"];
}

回答1:


I presume that your code is something like this:

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



回答2:


Do this server-side. Set the attribute checked="checked" in the radio button that is selected.



来源:https://stackoverflow.com/questions/7351725/auto-submit-a-radio-button-but-keep-it-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!