Passing parameter(s) with EventArgs
I was wondering if it were possible to pass parameters to a javascript event called from an AJAX extender (in this case AutoComplete) while preserving the EventArgs also being passed. Here's a short sample of what I mean:
<... OnClientItemSelected="DoThis(somevar, eventargs);" />
Script:
function DoThis(somevar, eventargs) {
var blah = somevar;
...
var blah2 = eventargs.get_text(开发者_JAVA技巧);
}
That's the idea. Does anyone know how to do this or if it's possible?
It's not exactly clear what your asking, but...I'll take a stab at it anyway. It sounds like you essentially want to augment the event data passed to the event handler. There are a number of ways to do this:
If you are using jQuery:
First, the bind()
method can be invoked with either 2 or 3 arguments:
bind( eventName , eventHandler )
bind( eventName , data , eventHandler )
If bind()
is invoked with 3 arguments, the second argument, data
, is an arbitrary object handed to the event handler as the .data
property of the Event
instance passed to the event handler.
Second, the event handler is a closure, the lexical scope of which includes those variables that were in scope at the point the closure was created. For instance, the usual way you would bind an event handler to an event in jQuery is something like this:
$(function(){
var pi = 3.1415926 ;
var phoneticAlphbet = { a = "alpha" , b = "bravo" , c = "charlie" ,
...
x = "x-ray" , y = "yankee" , z = "zulu"
} ;
$('input:radio[name=buttonset1]').click(function(){
// at this point, the variables pi and phonetic alphabet are in-scope,
// as well as any other globals you've created
// do something here
}) ;
}) ;
So that's a second way to pass data to the event handler.
A third way is to hang data off DOM tree elements. jQuery conveniently provides the data()
method to do this:
.data( name , value )
sets a value on the wrapped set.data( name )
retrieves values from the wrapped set
For example, this snippet will save the original value for each and element on the page:
$('input,select').each(function(){
var item = $(this) ;
item.data( 'original-value' , item.val() ) ;
}) ;
Having set it, the value can be retrieved:
$('input#password').change(function(){
var item = $(this) ;
var oldValue = item.data('original-value') ;
var newValue = item.val() ;
if ( oldValue == newValue )
{
alert('the new password must be different than the old password!') ;
}
}) ;
精彩评论