javascript - Using prototype to set an onclick event of a button? - Stack Overflow

I have this button:<button id="check_button" name ="check_button" type="but

I have this button:

<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')">
    <span>check</span>
</button>

Now I know I can access it using $('check_button') for example. Is there a way to set the setlocation parameter with Prototype?

I have this button:

<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')">
    <span>check</span>
</button>

Now I know I can access it using $('check_button') for example. Is there a way to set the setlocation parameter with Prototype?

Share Improve this question edited Jul 20, 2019 at 9:21 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 19, 2012 at 11:49 EOBEOB 3,08517 gold badges46 silver badges71 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
function doButtonClick(event, element) {
  console.log('event');
  console.log('element');

  // here, `element` is a reference to the button you clicked.
  var elementId = element.identify();

  // I'm not sure what setLocation takes in as a parameter, but...
  setLocation(elementId);
}

$('check_button').on('click', 'button', doButtonClick);

Here are the docs for the element.on: http://api.prototypejs/dom/Element/prototype/on/

Which is really just a quick way of creating a new Event.Handler: http://api.prototypejs/dom/Event/on/

For sure check out that second doc link - super useful stuff.

Yes,

$('check_button').observe('click',this.setLocation.bind(this,'...'));

setLocation: function(param)
{
alert(param);
}

In this way the param will be always what you pass. " bind " ia a vary powerfull method which holds that ability.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744300245a4567464.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信