0

私はjQueryが初めてで、テキストボックスに簡単なオートコンプリートを設定しようとしています。私のコードは以下です:

HTML:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />

<script>
$(function() {
    $( "#searchstr" ).autocomplete(
    {
         source:'searchjson.php',
    })

});
</script>

<?php 
include('code.php') ;
show_header();
?>

<title>CodeLib</title>
</head>
<body>

    <div><h3>Welcome to CodeLib.com.au</h3><div>
    <h4>Articles</h4>

    <form action="index.php" method="get">
        <div class="ui-widget" >Search:(4 chars min) <input name="searchstr" id="searchstr" type="text" /></div>
</form>
</body>

私のPHPコードは次のとおりです。

<?php
include("code.php");
// if the 'term' variable is not sent with the request, exit
if ( !isset($_REQUEST['searchstr']) )
    exit;

open_db();

// query the database table for zip codes that match 'term'
$rs = mysql_query('select article_title from articles where article_title like "'. mysql_real_escape_string($_REQUEST['searchstr']) .'%" limit 10');

// loop through each zipcode returned and format the response for jQuery
$data = array();
$str = "";
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array('label' => $row['article_title']);

    }
}

// jQuery wants JSON data
echo json_encode($data);
flush();

?>

注意事項:

固定値を使用してスクリプトをテストし、ソースを次のように変更しました。 source: availableTagsJSで固定値を割り当てます-正常に動作します

クエリ文字列で渡された文字列を使用して PHP をテストしたところ、次のような結果が得られました。

[{"label":"Simple HTML rich text editor for your web page"}]

しかし、問題は、ソースを php コードに割り当てるときに得られないことです。誰でも私に手がかりを与えることができますか??

どうもありがとう。

4

2 に答える 2

0

source-section の末尾にカンマがあると (特に IE を使用している場合)、簡単です。これを試してください、うまくいくかもしれません:

<script>
$(function() {
    $( "#searchstr" ).autocomplete(
    {
         source:'searchjson.php' //No comma at the end of this line
    })

});
</script>
于 2013-07-24T15:40:39.530 に答える
0

JavaScript エラーは発生しますか?

jQueryで使ってみよう

$(document).ready(function() {
    .. Your autocomplete code here
});

それ以外の$(function() {});

そうすれば、$('#searchstr')要素が存在します

于 2013-07-24T14:40:59.303 に答える