简体   繁体   中英

How to pass the textbox value in jquery autocomplete

I'm using jquery autocomplete in some textboxes in my web application (.Net 3.5). 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()

Can anyone please help me in solving this issue? Thanks

Try to use search event, in your example you are in bad scope.

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

To set option use 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.

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.

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. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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