场景:用户在 a 中输入名称Textbox
,结果通过jQueryUI
$("#textboxElement").autocomplete({...})
. 用户选择建议的结果之一(全名(用户名))并显示在#textboxElement
. 用户现在单击一个button
命名的“权限”,该“权限”应返回填充到预先存在的 HTML 表中的所选用户的权限列表。按钮单击应该接受用户选择,仅提取两个括号之间的最后一个用户名,并作为返回 Permission 对象列表的 web 服务的参数传递。
问题:页面上没有任何反应。没有显示错误。其他 jQUEeryUI 用户控件遍布整个页面并且不起作用。即使搜索也不适用于页面上的其他按钮单击事件。Ajax 代码给出错误(意外“。)我在哪里做错了?
jQueryUI代码:
$("#showPermission")
.button()
.click(function () {
var username = $('input:text[name=nameSearch]').val();
//extracting a string of text that lies between two (parenthesis) from the end of the string
var result = username.split('(');
for (var i = 1; i < result.length; i++) {
$("#txtSelectedUsername").val(result[i].split(')')[0]);
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Search.aspx/GetUserPermission",
data: "{'username':'" + $("#txtSelectedUsername").val() + "'}",
dataType: "json",
success: function (data)
{
$.each(data, function(key, val)
{
var row = $("<tr />");
$("<td />").text(val.username).appendTo(row);
$("<td />").text(val.level).appendTo(row);
$("<td />").text(val.location).appendTo(row);
$("<td />").text(val.role).appendTo(row);
row.appendTo("table.usersPermissionTbl");
});
},
error: function (xhr, textStatus, errorThrown)
{
var errorMessage = "Ajax error: " + this.url + " textStatus: " + textStatus + " errorThrown: " + errorThrown + " xhr.statusText: " + xhr.statusText + " xhr.status: " + xhr.status;
alert(errorMessage);
if (xhr.status != "0" || errorThrown != "abort")
{
alert(xhr.responseText);
}
}
});//end of ajax
});//end of click event
HTML
<table id="usersPermissionTbl" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Username</th>
<th>Level</th>
<th>Location</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
C# 代码
public static Permission[] GetUserPermission(string username)
{
List<Permission> allPermission = new List<Permission>();
SqlConnection con = new SqlConnection();
con.ConnectionString = connectionString;
string sqlString = "SELECT username, level, location, role from URTable WHERE username = '" + username + "'";
SqlDataAdapter sadp = new SqlDataAdapter(sqlString, con);
DataSet ds = new DataSet();
sadp.Fill(ds);
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dtrow in table.Rows)
{
Permission permission = new Permission();
permission.userName = dtrow["username"].ToString();
permission.level = dtrow["level"].ToString();
permission.location = dtrow["location"].ToString();
permission.role = dtrow["role"].ToString();
allPermission.Add(permission);
}
}
con.Close();
return allPermission.ToArray();
}
public class Permission
{
public string userName { get; set; }
public string level { get; set; }
public string location { get; set; }
public string role { get; set; }
}