简体   繁体   English

Html下拉提交表单并保留选项保留选项值

[英]Html Dropdown submit form and keep option retain option values

I am trying to create a simple dropdown box that filters a list into descending and ascending order. 我正在尝试创建一个简单的下拉框,将列表按降序和升序过滤。

The problem i am having is every time I submit the form or $_GET in this case the option I have selected in the drop down box resets. 我遇到的问题是每次我提交表单或$ _GET在这种情况下,我在下拉框中选择的选项重置。

I have used some php to change the selected option in the drop down but it isn't working and I am not sure why? 我已经使用了一些PHP来更改下拉列表中的选定选项,但它不起作用,我不知道为什么?

  <form name="query_form" method="get">
    <input type="hidden"/>
    <select name="the_filter" onchange="this.form.submit();">


    <option name="asc"  <?php if($_GET['the_filter']=="asc"){echo "selected=\"true\""; }if($_GET['the_filter']=="desc"){echo "selected=\"false\"";} ?> value="asc" >Price: ascending</option>
    <option name="desc"  <?php if($_GET['the_filter']=="desc"){echo "selected=\"true\""; }if($_GET['the_filter']=="asc"){echo "selected=\"false\"";} ?> value="desc" >Price: descending</option>
    </select>
    </form>

when i check the source code it says shows this 当我检查源代码时,它说明了这一点

 <form name="query_form" method="get">
    <input type="hidden"/>
    <select name="the_filter" onchange="this.form.submit();">


    <option name="asc"  selected="true" value="asc" >Price: ascending</option>
    <option name="desc"  selected="false" value="desc" >Price: descending</option>
    </select>
    </form>

however the dropdown box remains on descending even though the selected is set to true for ascending, it seems to be changing afterwards is this something to do with the onchange method???? 然而,即使将选中的值设置为true进行提升,下拉框仍保持降序,之后似乎正在改变这与onchange方法有关吗????

function Change(val) {

    document.query_form.query.value=val;
    document.query_form.submit();


}

thank you in advance for any help 预先感谢您的任何帮助

Can you try this, added selected='selected' instead of selected='true' or 'false' 你可以尝试这个吗,添加selected='selected'而不是selected='true' or 'false'

 <form name="query_form" method="get">
<input type="hidden"/>
    <select name="the_filter" onchange="this.form.submit();">
        <option name="asc"  <?php if($_GET['the_filter']=="asc"){echo "selected='selected'"; } ?> value="asc" >Price: ascending</option>
        <option name="desc"  <?php if($_GET['the_filter']=="desc"){echo "selected='selected'"; } ?> value="desc" >Price: descending</option>
    </select>
</form>

Hope this will work perfectly, 希望这将完美,

  <form name="query_form" method="POST">
<input type="hidden"/>
    <select name="the_filter" onchange="this.form.submit();">
        <option name="asc"  <?php echo ($_POST['the_filter'] == 'asc') ? ' selected="selected"' : ''; ?> value="asc" >Price: ascending</option>
        <option name="desc"   <?php echo ($_POST['the_filter'] == 'desc') ? ' selected="selected"' : ''; ?> value="desc" >Price: descending</option>
    </select>
</form>

This is happening because the php is producing broken HTML. 发生这种情况是因为php正在生成破坏的HTML。 It doesn't have to do with the javascript. 它与javascript无关。

<form name="query_form" method="get">
    <input type="hidden"/>
    <select name="the_filter" onchange="this.form.submit();">


    <option name="asc"  selected="selected" value="asc" >Price: ascending</option>
    <option name="desc" value="desc" >Price: descending</option>
    </select>
</form>

Notice how we are now using selected="selected" 注意我们现在如何使用selected =“selected”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM