Jquery令牌输入如何获取名称而不是id
我试着打电话给他,但似乎什么也没回音。。我做错什么了吗Jquery令牌输入如何获取名称而不是id,jquery,jquery-tokeninput,Jquery,Jquery Tokeninput,我试着打电话给他,但似乎什么也没回音。。我做错什么了吗 <script type="text/javascript"> $(document).ready(function() { $("input[type=button]").click(function () { alert("Would submit: " + $(this).siblings("input[type=te
<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: "john.doe@uhc.com", "value": "@" , "prefix": "Email1"},
{ id: 11, name: "j.doe@uhc.com", "value": "@" ,"prefix": "Email2"},
{ id: 13, name: "nancy.doe@uhc.com", "value": "@" ,"prefix": "Email3"},
{ id: 17, name: "liz.d@uhc.com", "value": "@", "prefix": "Email4" },
{ id: 19, name: "joe.doe@uhc.com", "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>
$(文档).ready(函数(){
$(“输入[类型=按钮]”)。单击(函数(){
警报(“将提交:”+$(this).sibbins(“输入[type=text]”).val());
});
});
$(文档).ready(函数(){
$(“#按钮1”)。单击(函数(){
var abc=$(“#演示输入本地”).tokenInput(“get”);
警报(abc);
警报(abc[7].名称);
var inputval=$(this.sillides)(“input[type=text]”).val();
警报(“输入值”+输入值);
警报(abc[inputval].name);
$(“#一些文本”).focusEnd();
$('some text').val($('some text').val()+''+inputval);
});
});
输入电话、电子邮件或http:
$(文档).ready(函数(){
$(“#演示输入本地”).tokenInput([
{id:7,姓名:“约翰。doe@uhc.com“,“值”:“@”,“前缀”:“Email1”},
{id:11,姓名:“j。doe@uhc.com“,“值”:“@”,“前缀”:“Email2”},
{id:13,姓名:“南希。doe@uhc.com“,“值”:“@”,“前缀”:“Email3”},
{id:17,姓名:“liz。d@uhc.com“,“值”:“@”,“前缀”:“Email4”},
{id:19,姓名:“乔。doe@uhc.com“,“值”:“@”,“前缀”:“Email5”},
{id:23,名称:“www.C#.com”,“值”:“http”,“前缀”:“Website1”},
{id:29,名称:“www.C.com”,“值”:“http”,“前缀”:“Website2”},
{id:31,名称:“www.Fortran.com”,“值”:“http”,“前缀”:“Website3”},
{id:37,名称:“www.VisualBasic.com”,“值”:“http”,“前缀”:“Website4”},
{id:41,名称:“www.DoC.com”,“值”:“http”,“前缀”:“Website5”},
{ID:43,名称:“www+c+.com”,“值”:“http”,“前缀”:“WebSITe6”},
{id:47,名称:“www.Java.com”,“值”:“http”,“前缀”:“Website7”},
{id:37,名称:“111-111-1111”,“值”:“#”,“前缀”:“Phone1”},
{id:41,名称:“222-222-2222”,“值”:“#”,“前缀”:“Phone2”},
{id:43,名称:“333-333-3333”,“值”:“#”,“前缀”:“Phone3”},
{id:47,名称:“555-555-5555”,“值”:“#”,“前缀”:“Phone4”}
], {
主题:“facebook”,
propertyToSearch:“值”,
hintText:“为电子邮件键入“@”,为网站键入“http”,为电话键入“#”,
tokenFormatter:函数(项){return“”+item.prefix+” “},
结果格式化程序:函数(项){return“”+item.prefix+“”+item.name+“ ”}
});
});
亲爱的,为什么不使用tokenInput自己的方法来获取对象
selector.tokenInput("get");
这就是如何从tokeninput(每个项都是{id:x,name:y}类型的对象)获取所选标记的数组。
进一步
如果您只想发出逗号分隔名称列表的警报,请尝试以下操作: 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列表
以上是非常原始的方法。您可以类似地循环通过.tokenInput(“get”)返回的数组代码>ABC指出的方法
假设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());
});
我用你的代码更新了我的代码。当我调用abc[0]时,它似乎没有返回任何值。名称我缺少什么吗?我在上面发布了我的代码-感谢您再次查阅tokenInput文档根据ABC的说法,我在上面的答案中添加了一些代码,这些代码循环通过.tokenInput(“get”)返回的数组代码>。哎哟,贴错地方了,请看我自己的答案。您已完全编辑了原始问题user244394!!感谢您看到我的代码,我正在尝试.tokenInput(“get”);但它没有返回任何var abc=selector.tokenInput(“get”);abc[0]。id;//对于第一个数组元素abc[0]。命名ami缺少什么?根据abc所说的,我在上面的答案中添加了一些代码,这些代码循环通过.tokenInput(“get”)返回的数组代码>。
$("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());
});