javascript - 在 C# 中选择事件和 WebMethod 的 Jquery 自动完成
问题描述
我正在使用jQuery autocomplete
并且工作正常。
现在我想使用用户选择的值并执行所需的操作。
在从预先填充的列表中选择值的上一步中,AutoComplete TextBox
我们将调用jQuery GetCustomerDetails
函数。
它将Country
名称作为输入参数,制作ajax call
细节WebMethod get Customer
并呈现在表格对象中。
这是正在使用的代码,我没有错误,但它不起作用,因为tblCustomers
表仍然是空的......
我该怎么做?
.aspx 页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function () {
$("#txtCountry").autocomplete({
source: function (request, response) {
var param = { keyword: $('#txtCountry').val() };
$.ajax({
url: "Default4.aspx/GetCountryNames",
data: JSON.stringify(param),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
select: function (event, ui) {
if (ui.item) {
GetCustomerDetails(ui.item.value);
}
},
minLength: 2
});
});
function GetCustomerDetails(country) {
$("#tblCustomers tbody tr").remove();
$.ajax({
type: "POST",
url: "Default4.aspx/GetCustomers",
data: '{country: "' + country + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
}))
},
failure: function (response) {
alert(response.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Country Name:
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</div>
<br />
<div>
<table id="tblCustomers" class="tblCustomers">
<thead>
<tr>
<th class="customerth">Name</th>
<th class="customerth">Population</th>
<th class="customerth">Code</th>
<th class="customerth">Continent</th>
<th class="customerth">Region</th>
<th class="customerth">GovernmentForm</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
</html>
.cs 页面
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
public class Customer
{
public string Name { get; set; }
public string Population { get; set; }
public string Code { get; set; }
public string Continent { get; set; }
public string Region { get; set; }
public string GovernmentForm { get; set; }
}
[WebMethod]
public static Customer[] GetCustomers(string country)
{
List<Customer> customers = new List<Customer>();
string CS = ConfigurationManager.ConnectionStrings["cn3"].ConnectionString;
string query = string.Format("SELECT `Name`, `Population`, `Code`, `Continent`, `Region`, `GovernmentForm` FROM `country` WHERE `Name` LIKE '%{0}%';", country);
using (MySqlConnection con =
new MySqlConnection(CS))
{
using (MySqlCommand cmd =
new MySqlCommand(query, con))
{
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Customer customer = new Customer();
customer.Name = reader.GetString(0);
customer.Population = reader.GetString(1);
customer.Code = reader.GetString(2);
customer.Continent = reader.GetString(3);
customer.Region = reader.GetString(4);
customer.GovernmentForm = reader.GetString(5);
customers.Add(customer);
}
}
}
return customers.ToArray();
}
[WebMethod]
public static string[] GetCountryNames(string keyword)
{
List<string> country = new List<string>();
string CS = ConfigurationManager.ConnectionStrings["cn3"].ConnectionString;
string query = string.Format("SELECT DISTINCT `Name` FROM `country` WHERE `Name` LIKE '%{0}%';", keyword);
using (MySqlConnection con =
new MySqlConnection(CS))
{
using (MySqlCommand cmd =
new MySqlCommand(query, con))
{
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
country.Add(reader.GetString(0));
}
}
}
return country.ToArray();
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
编辑问题
使用断点并调试此部分代码上的错误,
响应未定义
response($.map(data.d, function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
}))
解决方案
$.map(data.d, function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
})
解决方案
推荐阅读
- azure-devops - 如何在 Azure Devops 中发布对项目成员安全的 HTML 报告?
- amazon-web-services - 从 AWS S3 CLI 获取最后修改的对象
- r - 有条件地选择要在 R 中过滤的列
- sql-server - 带有表和运算符的 SQL CASE 语句
- github-actions - GitHub Action 内联 if
- r - 仅提取每行开头的数字
- sql - 如何在 MERGE 语句的 WHEN NOT MATCHED 部分插入 IF
- html - 是否有“任意 2 个元素之间”的选择器
- python - 如何从不同形状的二维数组(通过填充 NaN)创建形状相同的二维矩阵列表?
- swift - 在 while 循环中使用完成处理程序