I create a button dynamically on the page,using "<input
type=''button''
id="btnOk" onclick=fSubmit(''add'')
value="Add"/>"
now, i wanna let the button to trigger the fSubmit function with
another argument "update",that is
fSubmit(''update''),and set it''s value
to ''Update''.
i did it like this:
$("btnOk").value="Update";
$("btnOk").abserve("click",function(){fSubmit("update")});
it works!!But there''s one problem puzzling me.
I click the button,it firstly trigger fSubmit(''add''),and later
fSubmit(''update'').But my purpose is to trigger
fSubmit(''update'')....
Why?
And how can i reach my purpose?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-Mar-26 07:47 UTC
Re: Help me!About event handler using prototype.js
Observers are *added*, not *replaced*. So the second time over, you''ll
get add+update, of course.
You need to remove the first observer, which means keeping a reference
to it for later using with Event.stopObserving.
But actually, the simplest road is to NOT use these tricks, and observer
on fSubmit directly, with no parameter. Then have fSubmit look up
$(''btnOK'').value, and act accordingly. Also avoid inline
event handlers
like onclick, which won''t work properly on MSIE regarding event object
passing, and are an intrusion of behavior in content:
$(''btnOK'').observe(''click'', fSubmit); //
Only once, and on onclick= attr
function fSubmit() {
var mode = $(''btnOK'').value;
if (''Update'' == mode)
// ...
else
// ...
}
Of course, this fails when you start localizing the UI, but will you?
--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
How about just doing -
$("btnOk").value= "Update";
$("btnOk").onclick = function(){fSubmit("update");};
Thanks,
Mandy.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---