开发者

CodeIgniter dropdown (Javascript?)

Is there any way I can make a dropdown submit a form without clicking a button to to submit it. I want to be able to change languages on the fly in my site. I have the languages all set up. Here's my dropdown:

<?php echo form_open('languages'); 
    $language = array(
                        'select' => 'Select Language',
                        'english' => 'English',
                        'spanish' => 'Espa&ntilde;ol',
                        'german' => 'Deutsch',
                        'french' => 'Fran&ccedil;ais'
                    );

    echo form_dropdown('language', $language);
    echo form_hidden('current_page', uri_string());
    echo form_submit('submit', $开发者_StackOverflowthis->lang->line('header6'));
    echo form_close();
?>

How do I get the form to work without the submit button?


The easiest way would be to use jQuery. Your form and dropdown should have IDs to make referencing them easier.

$attributes = array('id' => 'form1');
echo form_open('languages', $attributes);
...
echo form_dropdown('language', $language, null, 'mydropdown');

Also include the following javascript (you'll need to load jQuery on the page for this to work). I've included Google's hosted version but you could install it locally if you prefer.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $("#mydropdown").live("change keyup", function () {
                $("#form1").submit();
            });
        });
    </script>

You don't strictly need to bind to keyup, you could just bind to the change event (in which case you could replace live("change") with simply change() but keyup covers keyboard changes to the dropdown.


I use onchange with any live select menu... makes it very very easy to fire off other methods.

<select id="myID" onchange="alert('Fired!')"><option value="gold">Beer</option>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜