简体   繁体   中英

Whats wrong with my jquery select

login_view.php

I am just trying to use some jQuery to select the a drop down I have created via code igniter's form helper. I have tried different javascript statements on the browser console but keep getting "undefined" for this form element. :(

<?php

    //build html for company drop down
    $form_options['--'] = "--";f

    foreach ($client_list as $client) {
        $form_options[$client['co_id']] = $client['co_name'];
    }

    $js = 'id="companies"';
    echo form_dropdown('', $form_options,'', $js);

?>
<input id="login" type="submit" value="Login">

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript">

$('#login').click(function() {
    var co_id = $(this).find('#companies').val();
    console.log(co_id);     
});


</script>

In this case there is not need to use find() because the element companies doesn't have #login as its parent. So you need to change it too

var co_id = $('#companies').val();

Just use

var co_id = $('#companies').val();

$(this).find('#companies').val(); you are trying to find element with id companies inside element with id login which doesn't exists .

Id's are unique so can call them directly using id-selector

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