简体   繁体   中英

How to submit a form with dropdown selected value only?

I want to submit a form which has just one dropdown with language values and i want to submit form and get access to that value so that i can localize some text on the page.

This is the code i am using but not able to access the post values

html form:

<form action='' method='post'>
<select id="lang">
    <option value="english">Eng</option>
    <option value="arabic">Arabic</option>
</select>
</form>

jQuery to submit the form:

 <script type="text/javascript">
    jQuery(function($){
        var lang = $("#lang").val();
        var Home = 'الرئيسية';

        $("#lang").change(function(){
            cntr = $("#lang option:selected").val();

            if(lang=="arabic"){
                $(".menu ul li a:eq(0)").text(Home);
            }
            $('form').submit();
        });
    });


</script>

I think you are missing a "name" attribute on the select:

<select name="lang" id="lang">
    <option value="english">Eng</option>
    <option value="arabic">Arabic</option>
</select>

您应该为“select”元素指定“name”属性

var lang = $("#lang").val(); will only fire when page loads and need name for select

Move it inside the change handler so it updates each time

   $("#lang").change(function(){
       var lang=$(this).val();
        cntr = $("#lang option:selected").val();

        if(lang=="arabic"){
            $(".menu ul li a:eq(0)").text(Home);
        }
        $('form').submit();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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