admin管理员组文章数量:1323330
I am using radio buttons to sort through paginated results. However, whenever the button is clicked and auto-submitted, it bees 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"];
}
I am using radio buttons to sort through paginated results. However, whenever the button is clicked and auto-submitted, it bees 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"];
}
Share
edited Nov 30, 2012 at 21:39
JSW189
asked Sep 8, 2011 at 16:55
JSW189JSW189
6,33512 gold badges46 silver badges73 bronze badges
1
- If you want to submit your form each time the user change something, you might like to use an ajax solution, so the page won't reload each time (it will give a better user experience). – NLemay Commented Nov 16, 2012 at 16:38
2 Answers
Reset to default 5I 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>
Do this server-side. Set the attribute checked="checked"
in the radio button that is selected.
本文标签: javascriptAutosubmit a radio button but keep it selectedStack Overflow
版权声明:本文标题:javascript - Auto-submit a radio button but keep it selected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139306a2422511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论