使用jQuery从文本框添加列表框中物品 [英] Using Jquery to add items in Listbox from Textbox
本文介绍了使用jQuery从文本框添加列表框中物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用jQuery从一个文本框中添加列表框中卡住的地方。
I am stuck somewhere using jquery to append the list box from a text box.
这是我的jQuery
here is my jquery
$("#btnAddSvc").click(function () {
var svc = $("#<%= txtServiceName.ClientID %>").val(); //Its Let you know the textbox's value
svc.appendTo("#<%=lstSvcName.ClientID %>");
});
我使用asp.net(C#)来开发我的code
I am using asp.net (c#) to develop my code
<asp:Button ID="btnAddSvc" runat="server" Text=">>" Font-Size="Medium" />
<asp:ListBox ID="lstSvcName" runat="server" SelectionMode="Multiple" ToolTip="Selected Service Names"
Width="169px"></asp:ListBox>
可以有人请帮助,因为我不能够得到列表框中的值。
can someone please help as i am not able to get the values in list box.
推荐答案
jQuery选择$()缺少#&LT;%= lstSvcName.ClientID%GT;
所以你会得到 ID
而不是对象
。
The jQuery selector $() is missing for "#<%=lstSvcName.ClientID %>"
so you will get id
of lstSvcName instead of object
.
我也改变了追加声明,因为它不具备正确的语法。
I also changed the append statement as it does not have correct syntax.
"#<%=lstSvcName.ClientID %>"
是
$("#<%=lstSvcName.ClientID %>")
您code将成为
$("#<%= btnAddSvc.ClientID %>").click(function () {
var svc = $("#<%= txtServiceName.ClientID %>").val(); //Its Let you know the textbox's value
$("#<%=lstSvcName.ClientID %>").append('<option value="'+svc+'">item '+svc+'</option>');
return false;
});
修改[由OP在列表框和文本框结算独特的项目要求提供更多的功能]
$("#<%= btnAddSvc.ClientID %>").click(function () {
var txt = $("#<%= txtServiceName.ClientID %>");
var svc = $(txt).val(); //Its Let you know the textbox's value
var lst = $("#<%=lstSvcName.ClientID %>");
var options = $("#<%=lstSvcName.ClientID %> option");
var alreadyExist = false;
$(options).each(function () {
if ($(this).val() == svc) {
alert("Item alread exists");
alreadyExist = true;
return;
}
txt.val("");
// alert($(this).val());
});
if(!alreadyExist)
$(lst).append('<option value="' + svc + '">' + svc + '</option>');
return false;
});
这篇关于使用jQuery从文本框添加列表框中物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文