简体   繁体   English

如何在 jquery 自动完成中传递文本框值

[英]How to pass the textbox value in jquery autocomplete

I'm using jquery autocomplete in some textboxes in my web application (.Net 3.5).我在我的 web 应用程序 (.Net 3.5) 的某些文本框中使用 jquery 自动完成功能。 My problem is the prefix text is always blank.我的问题是前缀文本始终为空白。 The correct value won't be assigned to it.不会为其分配正确的值。

function TextBoxAutoComplete(scope, controlId, contextKeyId) {

var txtbox = null;
var flagValue;
if (scope) {
    txtbox = $('input[id$="' + controlId + '"]', scope);
} else {
    txtbox = $('input[id$="' + controlId + '"]', document);
}

var contextKeyValue = $('input[id$="' + contextKeyId + '"]', document).val();

$(txtbox).autocomplete("../Handlers/MiscHandler.ashx", {
    minChars: 0,
    extraParams: { prefixText: $(this).val(), count: '10', contextKey: contextKeyValue, flag: 'codePart' },
    selectFirst: false,
    width: 49
}).result(function(event, data, formatted) { // result is a separate function
    var dummy = new Object();
    dummy.value = data[1];
    dummy.text = data[0];
    var test = new Test(dummy);
});
}

I call the above method at document ready.我在文档准备好时调用上述方法。 Here the problem is I don't get the textbox value (currently typed text) when i pass it to the variable 'prifixText' prefixText: $(this).val()这里的问题是,当我将文本框值(当前键入的文本)传递给变量“prifixText”时,我没有得到它prefixText: $(this).val()

Can anyone please help me in solving this issue?谁能帮我解决这个问题? Thanks谢谢

Try to use search event, in your example you are in bad scope.尝试使用search事件,在您的示例中,您处于错误状态 scope。

http://jqueryui.com/demos/autocomplete/#event-search http://jqueryui.com/demos/autocomplete/#event-search

To set option use http://jqueryui.com/demos/autocomplete/#method-option要设置选项,请使用http://jqueryui.com/demos/autocomplete/#method-option

Finally I was able to find out the answer with the help of my friend.终于在朋友的帮助下找到了答案。

From the handler, "../Handlers/MiscHandler.ashx" I tried to access the textbox text by accessing the value of 'prefixText' as follows.在处理程序“../Handlers/MiscHandler.ashx”中,我尝试通过访问“prefixText”的值来访问文本框文本,如下所示。

string prefixText = string.Empty;
if (context.Request["prefixText"] != null)
{
prefixText = context.Request["prefixText"].ToString();
}

But it didn't work, didn't allow me to get the textbox value.但它没有用,不允许我获取文本框值。 Instead of that I accessed the "q" parameter in the query string, which comes default.取而代之的是,我访问了查询字符串中的“q”参数,这是默认的。

if (context.Request["q"] != null)
{
prefixText = context.Request["q"].ToString();
}

This worked perfect and there was no issue with the jquery code, issue was how i handled the HTTP request.这工作完美,jquery 代码没有问题,问题是我如何处理 HTTP 请求。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM