簡體   English   中英

jQuery UI Autocomplete是否可以使用多個輸入字段中的值

[英]Can jQuery UI Autocomplete use values from multiple input fields

我已經在這段代碼中苦苦掙扎了一段時間,老實說,我被卡住了。

這是我的自動完成腳本的樣子:

jQ19(function(){
    // minLength:1 - how many characters user enters in order to start search
    jQ19('#location_name').autocomplete({

        source:'adress.php',
        minLength:2,
        select: function(event, ui){

            // just in case you want to see the ID
            var accountVal = ui.item.value;
            console.log(accountVal);

            // now set the label in the textbox
            var accountText = ui.item.label;
            jQ19('#location_name').val(accountText);

            return false;
        },
        focus: function( event, ui ) {
            // this is to prevent showing an ID in the textbox instead of name
            // when the user tries to select using the up/down arrow of his keyboard
            jQ19( '#location_name' ).val( ui.item.label );
            return false;
        }

    });

});

一些更多的js給我帶來額外的價值:

<script type='text/javascript'>
    $('#city').on( 'change', function () {
        var x = document.getElementById('city').value;
        $.ajax({
            type: 'post',
            url: 'http://localhost/profsonstage/account/_compettions/autocomplate/location_name.php',
            data: {
                value: x
            },
            success: function( data ) {
                console.log( data );
            }
        });
    });
</script>*/

和獲取數據結果的PHP:

try {
    $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
    echo "Connection error: " . $exception->getMessage();
}

// get the search term
$city = $_GET['city'];
$b = explode(" ",$city); //get rid of the city code
$a =$b[0];

$search_term = isset($_REQUEST['term']) ? $_REQUEST['term'] : "";

//query to search for data
$query = "SELECT * from locations where adress like '%$search_term%' and city=='$a'
LIMIT 0 , 5";

問題是,當用戶在輸入中為#City輸入值時,查詢看起來像這樣:

$ query =“ SELECT *從地址如'%%'和city ==' SomeCity'LIMIT 0,5的位置”;

當我們轉到下一個輸入(也應該也是自動完成並開始輸入)時,查詢看起來像

$ query =“ SELECT *從地址,例如'% 輸入到自動補全 %'和city ==” LIMIT 0,5“的位置;

基本上我無法找出一種同時傳遞值的方法。 任何幫助將不勝感激。

您可以使用以下內容;

jQ19(function(){
    // minLength:1 - how many characters user enters in order to start search
    jQ19('#location_name').autocomplete({

        minLength:2,
        select: function(event, ui){

            // just in case you want to see the ID
            var accountVal = ui.item.value;
            console.log(accountVal);

            // now set the label in the textbox
            var accountText = ui.item.label;
            jQ19('#location_name').val(accountText);

            return false;
        },
        source: function( request, response ) {
            var term = request.term;

            $.getJSON( "address.php?term=" + term + "&city=" + $("#city").val(), request, function( data, status, xhr ) {
              response( data );
            });
        },
        focus: function( event, ui ) {
            // this is to prevent showing an ID in the textbox instead of name
            // when the user tries to select using the up/down arrow of his keyboard
            jQ19( '#location_name' ).val( ui.item.label );
            return false;
        }

    });

});

並在您的php文件中;

try {
    $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
    echo "Connection error: " . $exception->getMessage();
}


// get the search term
$city = $_GET['city'];
$b = explode(" ",$city); //get rid of the city code
$a =$b[0];

$search_term = isset($_REQUEST['term']) ? $_REQUEST['term'] : "";

//query to search for data
$query = "SELECT * from locations where adress like '%$search_term%' and city=='$a'
LIMIT 0 , 5";

//Fetch your data and respond json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM