简体   繁体   English

如何使用Jquery在javascript onClick事件中传递参数?

[英]how to passing parameters in javascript onClick event using Jquery?

i want to ask, how to passing parameters in javascript onClick event using Jquery ? 我想问一下,如何使用Jquery在javascript onClick事件中传递参数?

I have been stuck on this problem. 我一直陷在这个问题上。 after i click the button, i cannot passing the parameters, but if i dont give parameters, its work. 单击按钮后,我无法传递参数,但是如果不提供参数,它将起作用。 what should i do ? 我该怎么办 ? sorry if my english is not good. 对不起,如果我的英语不好。

var ConfirmShowEventRef = firebase.database().ref().child("ConfirmEvent");``
ConfirmShowEventRef.on('child_added', snapshot => {
var bankName = snapshot.child("InMapRekening").val();
var rekNumber = snapshot.child("RekeningNumber").val();
var confirmAt = snapshot.child("confirmAT").val();
var eventTitle = snapshot.child("eventTitle").val();
var id = snapshot.key;

$("#table_transaction").append("<tr><td>"+ id +"</td><td>" + eventTitle + "</td><td><span class='label label-success'>" + bankName + "</span></td><td><div class='sparkbar' data-color='#00a65a' data-height='20'> " + rekNumber + " </div></td><td>" + confirmAt + "</td><td><button type='button' class='btn btn-sm btn-info btn-flat' name='button' id='"+ id +"' onClick='confirm("+ id +")'>Confirm</button></td></tr>");});function confirm(id){alert(id);}

from jQuery .on() documentation : http://api.jquery.com/on/ You know that the .on() method accept those params: 来自jQuery .on()文档: http ://api.jquery.com/on/您知道.on()方法接受这些参数:

.on( events [, selector ] [, data ], handler )

I'm not going to talk about the selector param because it is irrelevant here. 我将不讨论选择器参数,因为它与此处无关。

  • the first one if the which are simple strings identifying your event. 第一个(如果是简单的字符串来标识您的事件)。
  • handler a function invoked when the events occurs with the event object as a parameter (corresponding to your snapshot variable) here. 处理程序当事件发生时调用的函数,此处将事件对象作为参数(对应于您的snapshot变量)。
  • the second one, and that is your answer, because any data of any type that you pass here will be accessible from the data prop of the event object passed to you handler ( snapshot ) 第二个,这就是您的答案,因为您可以从传递给您的处理程序( snapshot )的事件对象的data属性中访问在此传递的任何类型的任何数据。

$DOMnode.on('event', { foo: 'bar' }, event => console.log(event.data));

Here event.data will be equal to {foo: 'bar'} ; 这里event.data将等于{foo: 'bar'} ;

There is another way of doing this. 还有另一种方法。 There is no need to pass parameters while invoking onclick event, Whatever values you want to use when click event is triggered can be added as attributes to the target element and read later. 调用onclick事件时无需传递参数,可以将触发click事件时要使用的任何值添加为目标元素的属性,并在以后读取。

<div id="click-sample" some-data="You got data inside Click"> Click Here </div>

$("#click-sample").click(function(e){

    alert($(e.target).attr('some-data'));

});

https://jsfiddle.net/ath78cfy/ https://jsfiddle.net/ath78cfy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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