jquery.ui自动完成范围问题 [英] jquery.ui autocomplete scope issues
问题描述
您好,我目前正在努力从自动填充中获取文本输入的值,以便在页面的其他部分中使用。我认为这可能是一个范围问题,但我对jquery并不坚定,所以我不知道在一个地方改变某些东西可能会破坏脚本的开头。
我想要做的是开始输入公司名称,让它自动完成,然后当我点击按钮时,我想要内容该文本框在其他地方填充标签。我还希望能够运行带有该数据的查询以放入数据表中,但是,我认为一个解决方案将成为另一个解决方案。
现在发生的事情是我无法访问公共静态列表中的任何内容,因为除了它之外的所有内容似乎都在外部范围内,所以假设文本框中甚至分配了一个值,我无论如何都无法访问它。 br />
这是一个正在播放的文件列表:
- svcfunctions.js
- servicehub.aspx
- servicehub.apsx.cs
从aspx的片段开始:
Hello, I'm currently struggling with getting the values of the text inputs from autocompletes to use in other portions of the page. I think it might be a scope thing, but I'm not solid on jquery, so I don't know how changing something in one place might break the script to begin with.
What I want to be able to do is to start typing in a company name, have it autocomplete, and then later when I click a button, I want the contents of that textbox to populate a label somewhere else. I also want to be able to run a query with that data to place into a data table, however, I think the solution to one will be the solution to the other.
What's happening now is that I cannot access anything from within the public static list, as everything beyond it appears to be in an outer scope, so assuming there's even a value assigned to the textbox, I can't reach it anyways.
Here's a list of the files in play:
- svcfunctions.js
- servicehub.aspx
- servicehub.apsx.cs
Starting with the snippet from the aspx:
</div> <div class="large-4 columns">
<label>Company:</label>
<div class="demo">
<div class="ui-widget">
<input type="text" id="txtSearch" class="autosuggest"/>
</div>
</div>
</div>
这是aspx.cs的代码片段(代码隐藏):
Here's the snippet from the aspx.cs (code-behind):
[WebMethod]
public static List<string> GetAutoCompleteData(string companyname)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=DATAX;Integrated Security=true;Initial Catalog=Archive"))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CardName from [Archive].[dbo].[Customers] WHERE CardName LIKE @SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", companyname);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["CardName"].ToString());
}
return result;
}
}
}
这是来自js的片段:
Here's the snippet from the js:
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
minLength: 3,
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ServiceHub.aspx/GetAutoCompleteData",
data: "{'companyname':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error: Invalid SQL Query in AutoComplete (CardName)");
}
});
}
});
}
自动完成工作正常,但我无法访问所选的或保持值位于文本框中。
The autocomplete works fine, but I can't access the selected or held value sitting in the text box.
推荐答案
(document).ready(function(){
SearchText();
});
函数SearchText(){
(document).ready(function () { SearchText(); }); function SearchText() {
(。autosuggest)。autocomplete({
minLength:3,
source:function(request,response) {
(".autosuggest").autocomplete({ minLength: 3, source: function (request, response) {
.ajax({
type:POST,
contentType:application / json; charset = utf-8,
url: ServiceHub.aspx / GetAutoCompleteData,
data:{'companyname':'+ document.getElementById('txtSearch')。value +'},
dataType:json,
成功:函数(数据){
响应(data.d);
},
错误:函数(结果){
alert(错误:自动完成(CardName)中的无效SQL查询);
}
});
}
});
}
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "ServiceHub.aspx/GetAutoCompleteData", data: "{'companyname':'" + document.getElementById('txtSearch').value + "'}", dataType: "json", success: function (data) { response(data.d); }, error: function (result) { alert("Error: Invalid SQL Query in AutoComplete (CardName)"); } }); } }); }
自动完成工作正常,但我无法访问所选的或保持价值坐在文本框中。
The autocomplete works fine, but I can't access the selected or held value sitting in the text box.
这篇关于jquery.ui自动完成范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!