在模型中提前输入 return 空值以形成输入字段

Typeahead in a Model return empty value to form input field

我创建了一个带有按钮的 table。当用户切换按钮时,它将打开 Bootstrap-Modal。 Modal 内部是一个带有 typeahed select 字段的表单。该列表有效,所有项目都 select 可用。

我的问题: 如果我 select 显示在 post 表单上的项目,则 return 值 (PHP $_POST) 为空。

我的js代码

$(document).ready(function () {
$('.typeahead').typeahead({
    source: function (query, process) {
        $.ajax({
            url: '/testPost',
            type: 'POST',
            dataType: 'JSON',
            //data: 'query=' + query,
            data: 'query=' + $('#contactName').val(),

            success: function(data)
            {
                var results = data.map(function(item) {
                    //var someItem = { contactname: item.ContactName, telephone: item.Telephone, email: item.Email };
                    var someItem = { contactname: item.ContactName, id: item.id };
                    console.log(item);
                    return JSON.stringify(someItem.contactname);
                });

                return process(results);
            }
        });
    },
    minLength: 1,
    updater: function(item) {
        // This may need some tweaks as it has not been tested
        var obj = JSON.parse(item);
        return item;
    }
});

});

php json

    $return_arr[] = array("id" => "1", "ContactName" => "Dooome");
$return_arr[] = array("id" => "2", "ContactName" => "Caa");
$return_arr[] = array("id" => "3", "ContactName" => "Veee");
$return_arr[] = array("id" => "4", "ContactName" => "Moo");
$return_arr[] = array("id" => "5", "ContactName" => "Reee");
#$return_arr[] = array("value" => "Dooome");

header("Content-Type: application/json");
print_r(json_encode($return_arr));

如果您希望输入 .val() 中的 select 不起作用,您可以通过以下方式获得价值:$('#contactName').typeahead('val'),这样您可以获得价值 用于发送 Post Ajax

数据的输入

看看the documentation of typeahead