PHP: on select change, post form to self

前端 未结 3 1575
迷失自我
迷失自我 2021-01-12 10:16

It\'s basically what the title says.. I have a form with a select control that I want to force the form to post back to self on change.

$bmsclientlist = $cl         


        
相关标签:
3条回答
  • 2021-01-12 10:24

    a quick fix is this:

    Add an onchange attribute to your selectlist

    <select name="bmsid" onchange="javascript: form.submit();">
    
    0 讨论(0)
  • 2021-01-12 10:28

    This is a <select> that will submit the parent form

    <form method="post" action="#" name="myform">
        <select name="x" onchange="myform.submit();">
            <option value="y">y</option>
            <option value="z">z</option>
        </select>
    </form>
    

    All you have to do is give a name to your <form> and add the onchange event to your <select>...


    Adam is right. While the example above works perfectly, I would do it like this:

    Using jQuery but there are many other options available...

    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script>
        $(document).ready(function(){
            $('#mySelect').change(function(){
                myform.submit();
            });
        });
    </script>
    </head>
    

    and the form

    <body>
    <form method="post" action="" name="myform">
        <select name="x" id="mySelect">
            <option value="y">y</option>
            <option value="z">z</option>
        </select>
    </form>
    </body>
    
    0 讨论(0)
  • 2021-01-12 10:29

    Change your select menu to this:

    <select name="bmsid" onchange="document.forms['changebmsid'].submit()">
    

    Update

    Here's another possible approach:

    <script type="text/javascript">
        window.onload = function() {
            document.forms['myform'].addEventListener('change', function() {
                this.submit();
            }, true);
        };
    </script>
    

    Put that anywhere on your page, and you can leave you form as it is.

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