Javascript 自动完成函数 href

标签 javascript html json autocomplete

我有以下 javascript 函数:

   <script type="text/javascript">

    $(function() {
        var data = (<?php include("php/search_new.php"); ?>).Data.Recipes;
        var source = [];
        for (var i in data) {
            source.push({"href": "/php/get_recipe_byID.php?id=" + data[i].ID, "label": data[i].TITLE});
        }
        $("#searchrecipes").autocomplete({
            minLength: 3,
            source: source,
            select: function(event, ui) {
                window.location.href = ui.item.href;
        }
    }); 
});

   </script> 

<input id="searchrecipes" type="text" name="searchrecipes" style="margin-left: 850px; margin-top: 0px; width:170px; background: #fff url(images/search_icon.png) no-repeat 100%;"  onblur="this.style.background='#ffffff';   background: #fff url(images/search_icon.png) no-repeat 100%;" onfocus="this.style.background='#c40606';  background: url(images/search_icon.png) no-repeat 100%;"  placeholder="Suchen..."></input>
                <input type="submit" name="buttonsenden" style="display:none;" value="" width: 5px></input>

该功能本来可以工作,但突然停止工作。问题是,下拉自动完成上的 href 不可单击。

var data = ({"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurstel"},"Recipe_9":{"ID":"9","TITLE":"Schnitzel"},"Recipe_10":{"ID":"10","TITLE":null},"Recipe_19":{"ID":"19","TITLE":null},"Recipe_20":{"ID":"20","TITLE":"Hundefutter"},"Recipe_26":{"ID":"26","TITLE":"Apfelstrudel"},"Recipe_37":{"ID":"37","TITLE":null},"Recipe_38":{"ID":"38","TITLE":"AENDERUNG"},"Recipe_39":{"ID":"39","TITLE":null},"Recipe_40":{"ID":"40","TITLE":"Schnitzel"},"Recipe_42":{"ID":"42","TITLE":"Release-Test"},"Recipe_43":{"ID":"43","TITLE":"Wurstel2"}}},"Message":null,"Code":200}).Data.Recipes;

所有必要的 jquery 脚本都可用。 可能是什么问题?

最佳答案

为什么不直接使用远程源,而不是让这个数组起作用? 这不是 PHP 和 JS 一起使用的方法:

var data = (<?php include("php/search_new.php"); ?>)

您可以直接使用远程源。它将通过 AJAX 请求加载:

$(function() {
        $("#searchrecipes").autocomplete({
            minLength: 3,
            source: "/php/recipe_index.php",
            select: function(event, ui) {
                window.location.href = ui.item.href;
            }
    });

参见Documentation "source:" parameterdemo-implementation (view sourcecode)

干杯。弗兰克

关于Javascript 自动完成函数 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8266789/

相关文章:

javascript - 将主体的数据属性值分配给变量

javascript - 我们可以使用 jquery 更改 html 或 body 高度吗?

java - 如何在servlet中获取jQuery JSON请求参数?

json - 以 Map<String, dynamic> 作为正文的 Dart HTTP POST

php - MySQL 数据处理。如何从数据库获取斜杠后面的值

javascript - 我可以突出显示选定的列表元素吗?

javascript - 元素类型无效 : expected string or class but got undefined

javascript - 使用 Vue.js 使必需的属性接受 null,但不是未定义

javascript - 使用用户 ID 链接到 Instagram 个人资料

HTML 5 视频通过 CSS 修改