我是JQuery新手。目前正在使用一个封闭的框架,该框架允许我添加代码,但不能更改某些行为。为了添加功能,我需要挂接到特定的Ajax请求并将其重定向到我的URL(这样我就可以在那里更改数据并成为代理),或者在客户端更改请求的参数,而且在更新文本输入框后会触发原始事件。 那我使用JQuery可以吗? 如果可以,该怎么办? 进一步检查后,似乎使用JQuery Autocomplete插件操纵了文本框。我正在尝试翻译此ajax请求返回的单词。通常来说,用户只会以一种语言输入文本,我想将其翻译成英语(翻译本身不是问题),因此自动完成功能将使用英语单词,然后我想将英语翻译成原始语言,以 示在文本框和悬停div中。
I don't think there is a simple solution to what you're trying to do since the XMLHTTPRequest object is pretty closed down for security reasons.
If you're lucky the part that does the AJAX request is broken out into a function that you can overwrite or override using the prototype object.
If not, you're probably better off hooking the change event of the textbox.
In jQuery:
$('#textboxid').unbind();
$('#textboxid').change(function () {
$.ajax({
url: 'http://yourwebservice',
data: {}, //the data to send to your webservice
success: function (data) {
//do your magic
};
})
});
Use the google translate jquery plugin. I've used it very successfully for translating text in the past: http://code.google.com/p/jquery-translate/
I have used the jQuery autocomplete plug-in to do something similar, although not in a scenario where I am dealing with a "closed framework".
You will be able to easily accomplish what you want by using the .setOptions() method on the autocomplete object after it has been initialized. So your code will go something like this (untested! pseudocode!):
$(autoCompletedField).setOptions({
formatItem: function(resultsRow, resultPosition, totalResults, inputWord) {
// translate your menu items to be displayed in the drop down menu here
// perhaps extend jQuery to handle $.translate(inputWord) ?
return "<li>"+translatedWord+"</li>";
}
}).result(function(event,selectedItem){
// optionally do something more when the user selects one of those items here
// translate the selectedItem back to another language? i dont know.
return translatedItem;
});
check out the options: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
You could look at implementing your code in a "beforeSend" of the AJAX request?
Have you taken a look at the observer pattern in javascript? I have created an example on my blog. This might help you.
http://www.versomas.com/dannyg/post/Observer-pattern-in-Javascript.aspx