【问题标题】:Display the name but submit the ID with jQuery tag-it显示名称但使用 jQuery tag-it 提交 ID
【发布时间】:2015-06-30 17:33:49
【问题描述】:

感谢您的阅读和帮助!

我正在使用 jQuery“tag-it”插件:https://github.com/aehlke/tag-it

我已经检查了网站上当前的问题和答案,但它们并没有帮助实现我所需要的 100%。我需要最后一步的帮助。

我想做的是,当用户写电子邮件地址时,系统会建议哪些用户属于该地址。

建议是在 PHP 页面中生成的,我使用插件中的 AJAX 获取它们。给出的格式是这样的:

[
 {"value": "13", "label": "Mauricio"},
 {"value": "4", "label": "Manolo"}
]

“value”是用户的ID,“label”是人的名字。

在示例中,“Manalo”和“Mauricio”显示为可供选择的建议。但是当它们被选中时,显示的是用户的 ID(“值”),而不是名称(“标签”)。

我想要做的是显示所选用户的名称,但是当我发送表单时,只发送 ID。

这是当前代码。再次非常感谢您的帮助。

$(".users").tagit({
    autocomplete: {
        source: function(request, response) {
            $.ajax({
            type: "GET",
            url: 'test_json.php?text=' + request.term,
            dataType: "json",
            success: function(data) {

                response( $.map( data, function( item ) {
                    return {
                        label: item.label,
                        value: item.value
                    }
                }));
            }
        });
    },
    },
});

【问题讨论】:

  • 如何发送数据???
  • 不知道你的意思。
  • @Fernan 你有解决方案吗?如果你知道了,请与我分享
  • 不,我无法让它工作。我只是将插件更改为 select2,这不是我正在寻找的解决方案,但至少它可以工作。

标签: javascript jquery jquery-ui multi-select tag-it


【解决方案1】:

从 jquery 自动完成中获取选中的选项值

$(".users").tagit({
    autocomplete: {
        source: function(request, response) {
            $.ajax({
            type: "GET",
            url: 'test_json.php?text=' + request.term,
            dataType: "json",
            success: function(data) {

                response( $.map( data, function( item ) {
                    return {
                        label: item.label,
                        value: item.value
                    }
                }));
            },
select: function(event, ui) {
         //For better understanding kindly alert the below commented code

         var selectedObj = ui.item;
         alert(selectedObj.value);
    }

        });
    },
    },
});

【讨论】:

  • 我试过了,但它什么也没做。就像参数不存在一样。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多