简体   繁体   English

从按钮调用jquery函数(不使用onclick)

[英]call jquery function from button (not using onclick)

I made a function in jQuery to edit database information based on ID from a specific table. 我在jQuery中创建了一个函数,用于基于特定表中的ID编辑数据库信息。

clickme(id);

That function was called in the onclick event but it seems that's a bad pratice. 该函数在onclick事件中被调用,但这似乎是一个糟糕的实践。 My code to call the function was created in PHP so it lists automatically the ID in the function. 我的调用该函数的代码是用PHP创建的,因此它会自动列出该函数中的ID。 For example, it fetches the database and list all ID's available, creating automatically a link based on ID: 例如,它获取数据库并列出所有可用的ID,并根据ID自动创建一个链接:

<button ... onclick='clickme(1)'...</button>
<button ... onclick='clickme(2)'...</button>
<button ... onclick='clickme(3)'...</button>

Since it's a bad pratice, how can I make this same working method without using the onclick method? 由于这是很糟糕的做法,我如何不使用onclick方法而做出相同的工作方法? The objetive is using something like sweetalert to: 该对象使用诸如sweetalert之类的东西来:

Click the button that contains the id and call the function
Show a sweetalert to user confirm the action in the id X
Confirm and call the function
Show another sweetalert to show OK or NOK

Anyone can help me in this? 有人可以帮我吗? It's working with the onclick method, but I'd rather prefer to user another method, but I don't know how can I call the function with the "ID" I need too without this method. 它与onclick方法一起使用,但是我宁愿使用另一种方法,但是我不知道如果没有该方法,我怎么也需要带有“ ID”的函数。 Thanks. 谢谢。

You can add an EventListener to all buttons and run the function clickme inside it with a number you get from a data attribute: 您可以将EventListener添加到所有按钮,并使用从data属性获得的数字在其中运行clickme函数:

 $("button").click(function() { let button = $(this); // the button you clicked let id = button.data("id"); // the data-id attribute of the button clickme(id); }); function clickme(id) { console.log(`Button with id ${id} clicked`); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button data-id="1">Click Me 1</button> <button data-id="2">Click Me 2</button> <button data-id="3">Click Me 3</button> <button data-id="4">Click Me 4</button> 

This way you can still control the ID from PHP, but are not assigning listeners as attributes. 这样,您仍然可以从PHP控制ID,但不将侦听器分配为属性。

The buttons can also have multiple data- attributes: 这些按钮还可以具有多个data-属性:

 $("button").click(function() { let button = $(this); // the button you clicked let id = button.data("id"); // the data-id attribute of the button let fun = button.data("function"); clickme(id, fun); }); function clickme(id, fun) { console.log(`Button with id ${id} and function ${fun} clicked`); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button data-id="1" data-function="update">Click Me 1</button> <button data-id="2" data-function="active">Click Me 2</button> <button data-id="3" data-function="foo">Click Me 3</button> <button data-id="4" data-function="bar">Click Me 4</button> 

Use jQuery.click() 使用jQuery.click()

Give an ID to your input element. 给您的输入元素一个ID。 Look at below example. 看下面的例子。

<div id="target">
  Click here
</div>

$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});

You're correct in that using onclick should be avoided where possible. 您是正确的,应尽可能避免使用onclick Instead, use unobtrusive event handlers. 而是使用不引人注目的事件处理程序。 You can store the relevant element meta data in a data attribute which you can read in the function itself instead of passing it as an argument. 您可以将相关元素的元数据存储在data属性中,您可以在函数本身中读取该属性,而不必将其作为参数传递。

In JS it would look like this: 在JS中,它看起来像这样:

 Array.from(document.querySelectorAll('.clickme')).forEach(function(el) { el.addEventListener('click', function() { var foo = this.dataset['foo']; console.log(foo); // perform your logic here... }); }); 
 <button class="clickme" data-foo="1">#1</button> <button class="clickme" data-foo="2">#2</button> <button class="clickme" data-foo="3">#3</button> 

In jQuery it would be this: 在jQuery中将是这样的:

 $('.clickme').click(function() { var foo = $(this).data('foo'); console.log(foo); // perform your logic here... }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="clickme" data-foo="1">#1</button> <button class="clickme" data-foo="2">#2</button> <button class="clickme" data-foo="3">#3</button> 

You can also use the onclick function if this doesn't work 如果这不起作用,您还可以使用onclick函数

$(document).on('click','.clickme',function(){
  let button = $(this); // the button you clicked
  let id = button.data("id"); // the data-id attribute of the button
  clickme(id);
});


function clickme(id) {
  console.log(`Button with id ${id} clicked`);
}

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

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