简体   繁体   English

通过jQuery / ajax将多维JSON数据放入不同的输入文本框中

[英]multidimensional json data into different input text boxes through jquery/ajax

I have created an autocomplete searchbox followed by two input text boxes. 我创建了一个自动完成搜索框,后跟两个输入文本框。

Please help me how to assign values of autocomplete's selected values. 请帮助我如何分配自动完成功能的选定值。

For ex. 对于前。 if we select ajax, corresponding price should be assigned to 1003 and role should be assigned to foreign. 如果我们选择ajax,则应将相应的价格分配给1003,将角色分配给国外。

My json response which I got from php echo output is : 我从php echo输出得到的json响应是:

{
    "users": [
        {
            "name": "ajax",
            "price": "1003",
            "author": "foriegn"
        },
        {
            "name": "jquery",
            "price": "1000",
            "author": "dd"
        },
        {
            "name": "mysql",
            "price": "1000",
            "author": "f"
        },
        {
            "name": "php",
            "price": "1000",
            "author": "abc"
        },
        {
            "name": "php frameword",
            "price": "1000",
            "author": "def"
        },
        {
            "name": "php tutorial",
            "price": "1000",
            "author": "ghi"
        },
        {
            "name": "wordpress",
            "price": "1000",
            "author": "g"
        },
        {
            "name": "wordpress theme",
            "price": "1000",
            "author": "h"
        },
        {
            "name": "xml",
            "price": "1000",
            "author": "j"
        }
    ]
}   

My JavaScript code below : 我的JavaScript代码如下:

var counter = 2;
var availableTags = ["php", "php frameword", "php tutorial", "jquery", "ajax", "mysql", "wordpress", "wordpress theme", "xml"];

$("#addButton").click(function () {

    var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);

    var roleInput = $('<input/>', {
        type: 'text',
        placeholder: 'Role',
        name: 'Role' + counter,
        id: 'textbox' + counter
    });

    var priceInput = $('<input/>', {
        type: 'text',
        placeholder: 'price',
        name: 'price' + counter,
        id: 'textbox' + counter
    });

    var searchInput = $('<input/>', {
        type: 'text',
        placeholder: 'search',
        name: 'search' + counter,
        id: 'se' + counter
    });

    var hidd = $('<input/>', {
        type: 'hidden',
        name: 'searchhid' + counter,
        id: 'searchhid' + counter
    });



    newTextBoxDiv.append(searchInput).append(roleInput).append(priceInput).append(hidd);

    newTextBoxDiv.appendTo("#TextBoxesGroup");

    $('#se' + counter).autocomplete({
        source: availableTags
    });
    counter++;
});    

Current fiddle setup is at : http://jsfiddle.net/premgowda/UC74L/6/ 当前的小提琴设置位于: http : //jsfiddle.net/premgowda/UC74L/6/

PS: I am extremely new to Json and jquery. PS:我对Json和jquery非常陌生。 I havent added the json output into my fiddle setup as I am not sure how to do it . 我还没有将json输出添加到我的小提琴设置中,因为我不确定该怎么做。

Use jQuery UI autocomlete select event: 使用jQuery UI自动完成选择事件:

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

Once you've got the selected value, find the matching object and select it's others properties. 获得选定的值后,找到匹配的对象并选择它的其他属性。 Like this: 像这样:

for(i = 0; i < users.length; ++i) {
        (users[i].name == selected) {
        break;
        }
}

price = users[i].price;
author = users[i].author;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM