简体   繁体   English

用jQuery打开具有不同数据属性的按钮

[英]Open button with different data-attribute with jquery

I am having a table with 3 buttonsand I am using jquery-"3.2.1" . 我有一个带有3个按钮的表,并且正在使用jquery- jquery-"3.2.1"

I am trying to use the below javascript-file Builder to load the proper data later in the if statements. 我正在尝试使用下面的javascript文件Builder在if语句中稍后加载正确的数据。

My expected output is: 我的expected output是:

If pressing f.ex.: the Add CPU button, then the alert message console.log("cpu clicked") should appear. 如果按f.ex .: Add CPU按钮,则应显示警报消息console.log("cpu clicked")

Below you can find my minimum viable example: 您可以在下面找到我的最小可行示例:

 class Builder { constructor() { this.events(); } // end constructor events() { $(".btn btn-primary btn-sm").on("click", this.ourClickDispatcher.bind(this)); } // methods ourClickDispatcher(e) { var currentButton = $(e.target).closest(".btn btn-primary btn-sm"); console.log("test") if (currentButton.data('exists') == 'cpu') { console.log("cpu clicked") } if (currentButton.data('exists') == 'motherboard') { console.log("motherboard clicked") } if (currentButton.data('exists') == 'graphic-card') { console.log("graphic-card clicked") } } } export default Builder; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table style="float: left;" class="table table-bordered"> <tbody> <tr> <td>CPU</td> <td> <button type="button" data-cpu="cpu" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModal"> Add CPU </button> </td> </tr> <tr> <td>Motherboard</td> <td> <button type="button" data-motherboard="motherboard" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModal"> Add Motherboard </button> </td> </tr> <tr> <td>Graphic Card</td> <td> <button type="button" data-graphicCard="graphic-card" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModal"> Add Graphic Card </button> </td> </tr> </tbody> </table> 

As you can see all 3 buttons have the same class attribute, but differentiate in the data- attribute to load the proper output. 如您所见,所有3个按钮都具有相同的class属性,但是要区分data-属性以加载正确的输出。

Any suggestions why the below script does not give me the expected output if any of the buttons is clicked? 有什么建议,如果单击任何按钮,为什么以下脚本没有给我expected output

I appreciate your replies! 感谢您的答复!

If with $(".btn btn-primary btn-sm") you want to target the element with all these classes then you need to use 如果使用$(".btn btn-primary btn-sm")来定位所有这些类的元素,则需要使用

$(".btn.btn-primary.btn-sm")

( no spaces and a . for each class, that is is the correct css syntax ) 每个类都没有空格和. ,这是正确的css语法

Secondly, your data attributes ( according to your script ) should all be named data-exists with a different value, and not a differently named attribute for each. 其次,您的数据属性( 根据您的脚本 )应全部以不同的值命名为data-exists ,而不是为每个属性命名不同。

so 所以

<button type="button" data-exists="cpu"  ...> 
<button type="button" data-exists="motherboard"  ...>
<button type="button" data-exists="graphic-card"  ...>

if you indeed wanted to check for the existance of that attribute you should use 如果您确实想检查该属性的存在,则应使用

if (currentButton[0].dataset.cpu) {...}
if (currentButton[0].dataset.motherboard) {...}
if (currentButton[0].dataset.graphicCard) {...}

or if you have to use jQuery ( browsers not supporting dataset ) use the .is method 或者如果您必须使用jQuery( 浏览器不支持数据集 ),请使用.is方法

if (currentButton.is('[data-cpu]')) {...}
// etc

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

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