Auto-submit a radio button but keep it selected

前端 未结 2 624
失恋的感觉
失恋的感觉 2021-01-23 10:48

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

相关标签:
2条回答
  • 2021-01-23 10:55

    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>
    
    0 讨论(0)
  • 2021-01-23 10:55

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

    0 讨论(0)
提交回复
热议问题