简体   繁体   中英

Get value of hidden input populated by jQuery autocomplete

I am using a jQuery Autocomplete to populate a list of names from a database and then assign the compId number to a hidden input.

I want to be able to access the compId value as soon as the user selects a company and assign it to a variable that I can use in conjunction with PHP and a MySQL statement prior to the user moving on.

Here is the jQuery:

$(function() {
    function log( message ) {
        $("#compId").val(message);
        $( "#compId" ).scrollTop( 0 );
                        }
    $( "#companyForm" ).autocomplete({
        source: "/autoComp/companies.php",
        minLength: 2,//search after two characters
        select: function( event, ui ) {
            log( ui.item ? ui.item.id : "");
        }
    });
});

HTML:

<input type="hidden" id="compId" name="compId" required />

The autocomplete is working fine, just don't know how to access it immediately after the value is filled.

Thanks!

Apologies if I misunderstood what you are asking but there is a response event you can chain on to your autocomplete call like so:

http://api.jqueryui.com/autocomplete/#event-response

    $(function() {
        function log( message ) {
            $("#compId").val(message);
            $( "#compId" ).scrollTop( 0 );
                            }
        $( "#companyForm" ).autocomplete({
            source: "/autoComp/companies.php",
            minLength: 2,//search after two characters
            select: function( event, ui ) {
                log( ui.item ? ui.item.id : "");
            },
            response: function () {
             alert($("#compId").val());
            }
        });
    });

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