使ってます
入力フィールドにタグを作成しますが、入力フィールドにタグを追加した後、入力フィールドからラベルと値を取得したいのですが、
htmlコードは:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="Scripts/tag-it.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css"/>
<link href="Content/jquery.tagit.css" rel="stylesheet" />
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#mytags").tagit({
tagSource: function (request, response) {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetKeyword",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
if (data.d != null) {
response($.map(data.d, function (item) {
return {
label: item.Name,
Value: item.Id
}
}));
}
}
});
}
});
});
function myfunction() {
var tagStr = $("#mytags").tagit("tags")
alert("Got tags: " + tagStr);
$('#hiddenTags').val(tagStr);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
TO :
</div>
<ul id="mytags">
</div>
<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Unnamed1_Click" OnClientClick="myfunction();"/>
</form>
</body>
</html>
そしてC#コードは:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<Info> GetKeyword(string match)
{
var qry = GetInfo().Distinct().Where(k => k.Name.ToLower().StartsWith(match.ToLower()));
return qry.ToList();
}
public static IEnumerable<Info> GetInfo()
{
List<Info> list = new List<Info>();
list.Add(new Info { Id = 1, Name = "Tom", Email = "Tom@Email.com" });
list.Add(new Info { Id = 2, Name = "Torex", Email = "Torex@Email.com" });
list.Add(new Info { Id = 3, Name = "Tresa", Email = "Tresa@Email.com" });
list.Add(new Info { Id = 4, Name = "Morgan", Email = "Morgan@Email.com" });
list.Add(new Info { Id = 5, Name = "Paris", Email = "Paris@Email.com" });
list.Add(new Info { Id = 6, Name = "Patrick", Email = "Patrick@Email.com" });
return list;
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
}
}
public class Info
{
public int Id { get; set; }
public string Name { get; set; }
public String Email { get; set; }
}
ボタンをクリックしてタグ値を取得すると:
[送信]ボタンから値を取得することを期待していたのは、morgan、tom、patrick with "id"