Jquery Tokeninput如何获取名称而不是id

发布于 2025-01-04 09:08:10 字数 4578 浏览 1 评论 0原文

我尝试调用它,但它似乎没有返回任何内容..我做错了什么吗?

     <script type="text/javascript">
            $(document).ready(function() {
                $("input[type=button]").click(function () {
                    alert("Would submit: " + $(this).siblings("input[type=text]").val());
                });
            });


    $(document).ready(function() {
    $("#button1").click(function() {


                    var abc = $('#demo-input-local').tokenInput("get");
                    alert(abc);
                    alert(abc[7].name);

       var inputval =$(this).siblings("input[type=text]").val();
       alert("inputval " +inputval);

alert(abc[inputval].name);

      $("#some-text").focusEnd();

       $('#some-text').val($('#some-text').val()+'  ' + inputval);

    });
    });

            </script>

    <label> Enter Phone or Email or http: </label><input type="text" id="demo-input-local" name="blah" /><input type="button" id="button1" value="Submit" />
                                        <script type="text/javascript">
                                            $(document).ready(function () {
                                                $("#demo-input-local").tokenInput([
                                                { id: 7, name: "[email protected]", "value": "@" , "prefix": "Email1"},
                                                { id: 11, name: "[email protected]", "value": "@" ,"prefix": "Email2"},
                                                { id: 13, name: "[email protected]", "value": "@" ,"prefix": "Email3"},
                                                { id: 17, name: "[email protected]", "value": "@", "prefix": "Email4" },
                                                { id: 19, name: "[email protected]", "value": "@", "prefix": "Email5" },
                                                { id: 23, name: "www.C#.com", "value": "http", "prefix": "Website1" },
                                                { id: 29, name: "www.C.com", "value": "http", "prefix": "Website2" },
                                                { id: 31, name: "www.Fortran.com", "value": "http", "prefix": "Website3" },
                                                { id: 37, name: "www.VisualBasic.com", "value": "http", "prefix": "Website4" },
                                                { id: 41, name: "www.DoC.com", "value": "http", "prefix": "Website5" },
                                                { id: 43, name: "www.C++.com", "value": "http" , "prefix": "Website6"},
                                                { id: 47, name: "www.Java.com", "value": "http" , "prefix": "Website7"},  
                                                { id: 37, name: "111-111-1111", "value": "#", "prefix": "Phone1" },
                                                { id: 41, name: "222-222-2222", "value": "#", "prefix": "Phone2" },
                                                { id: 43, name: "333-333-3333", "value": "#" , "prefix": "Phone3"},
                                                { id: 47, name: "555-555-5555", "value": "#" , "prefix": "Phone4"}


                                             ], {
                                                 theme: "facebook",

                                                 propertyToSearch: "value",
                                                 hintText: "Type '@' for email or 'http' for website  or '#' for phone",
                                                 tokenFormatter: function (item) { return "<li><p>" +  item.prefix + "</p></li>" },
                                                 resultsFormatter: function (item) { return "<li>" + item.prefix +" "+ item.name + "</li>" }
                                             });
                                            });
                                        </script>
                                    </div>

I tried calling that, but it doesn't seem to return anything.. Am I doing something wrong?

     <script type="text/javascript">
            $(document).ready(function() {
                $("input[type=button]").click(function () {
                    alert("Would submit: " + $(this).siblings("input[type=text]").val());
                });
            });


    $(document).ready(function() {
    $("#button1").click(function() {


                    var abc = $('#demo-input-local').tokenInput("get");
                    alert(abc);
                    alert(abc[7].name);

       var inputval =$(this).siblings("input[type=text]").val();
       alert("inputval " +inputval);

alert(abc[inputval].name);

      $("#some-text").focusEnd();

       $('#some-text').val($('#some-text').val()+'  ' + inputval);

    });
    });

            </script>

    <label> Enter Phone or Email or http: </label><input type="text" id="demo-input-local" name="blah" /><input type="button" id="button1" value="Submit" />
                                        <script type="text/javascript">
                                            $(document).ready(function () {
                                                $("#demo-input-local").tokenInput([
                                                { id: 7, name: "[email protected]", "value": "@" , "prefix": "Email1"},
                                                { id: 11, name: "[email protected]", "value": "@" ,"prefix": "Email2"},
                                                { id: 13, name: "[email protected]", "value": "@" ,"prefix": "Email3"},
                                                { id: 17, name: "[email protected]", "value": "@", "prefix": "Email4" },
                                                { id: 19, name: "[email protected]", "value": "@", "prefix": "Email5" },
                                                { id: 23, name: "www.C#.com", "value": "http", "prefix": "Website1" },
                                                { id: 29, name: "www.C.com", "value": "http", "prefix": "Website2" },
                                                { id: 31, name: "www.Fortran.com", "value": "http", "prefix": "Website3" },
                                                { id: 37, name: "www.VisualBasic.com", "value": "http", "prefix": "Website4" },
                                                { id: 41, name: "www.DoC.com", "value": "http", "prefix": "Website5" },
                                                { id: 43, name: "www.C++.com", "value": "http" , "prefix": "Website6"},
                                                { id: 47, name: "www.Java.com", "value": "http" , "prefix": "Website7"},  
                                                { id: 37, name: "111-111-1111", "value": "#", "prefix": "Phone1" },
                                                { id: 41, name: "222-222-2222", "value": "#", "prefix": "Phone2" },
                                                { id: 43, name: "333-333-3333", "value": "#" , "prefix": "Phone3"},
                                                { id: 47, name: "555-555-5555", "value": "#" , "prefix": "Phone4"}


                                             ], {
                                                 theme: "facebook",

                                                 propertyToSearch: "value",
                                                 hintText: "Type '@' for email or 'http' for website  or '#' for phone",
                                                 tokenFormatter: function (item) { return "<li><p>" +  item.prefix + "</p></li>" },
                                                 resultsFormatter: function (item) { return "<li>" + item.prefix +" "+ item.name + "</li>" }
                                             });
                                            });
                                        </script>
                                    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

定格我的天空 2025-01-11 09:08:10

如果您只想提醒出逗号分隔的名称列表,请尝试以下操作:

javaScript:

$(document).ready(function() {
    $("input[type=button]").click(function() {
        var names = [];
        $(this).siblings("ul").find('li p').each(function(){
            names.push($(this).html());
            //names.push($(this).text());//possibly better
        });
        alert("Names are: " + names.join());
    });
});

但是,据我所知,实际提交的值仍然是逗号分隔的 id 列表。

以上是非常原始的方法。您可以类似地循环遍历 ABC 指出的 .tokenInput("get"); 方法返回的数组。

假设 ABC 关于 .tokenInput("get"); 的说法是正确的,那么这应该有效:

javascript:

$("input[type=button]").click(function() {
    var arr = $(this).siblings("input[type=text]").tokenInput("get");//Returns (we think) an array of objects
    var names = [];
    $.each(arr, function(i, obj){
        names.push(obj.name);//build an array of just the names
    });
    alert("Names are: " + names.join());
});

If you just want to alert out a comma-separated list of names, try this:

javaScript:

$(document).ready(function() {
    $("input[type=button]").click(function() {
        var names = [];
        $(this).siblings("ul").find('li p').each(function(){
            names.push($(this).html());
            //names.push($(this).text());//possibly better
        });
        alert("Names are: " + names.join());
    });
});

But, as far as I can tell, the value actually submitted will still be a comma-separated list of ids.

Above is very raw approach. You could similarly loop through the array returned by the .tokenInput("get"); method pointed out by ABC.

On the assumption that what ABC says about .tokenInput("get"); is correct then this should work:

javascript:

$("input[type=button]").click(function() {
    var arr = $(this).siblings("input[type=text]").tokenInput("get");//Returns (we think) an array of objects
    var names = [];
    $.each(arr, function(i, obj){
        names.push(obj.name);//build an array of just the names
    });
    alert("Names are: " + names.join());
});
执妄 2025-01-11 09:08:10

亲爱的,为什么不使用 tokenInput 自己的方法来获取对象

selector.tokenInput("get");

这就是从 tokeninput 中获取所选标记数组的方法(每个项目都是 {id: x, name: y} 类型的对象)。
更远

 var abc = selector.tokenInput("get");
 abc[0].id; // for first array element
 abc[0].name

Dear why not you use tokenInput's own method to get the object

selector.tokenInput("get");

This is how you Get the array of selected tokens from the tokeninput (each item being an object of the kind {id: x, name: y}).
Further

 var abc = selector.tokenInput("get");
 abc[0].id; // for first array element
 abc[0].name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文