获取数据价格jQuery [英] Get data-price jquery

查看:101
本文介绍了获取数据价格jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何用jquery从onclick图片中得出数据价格的总和?

I want to ask how can i sum data-price from images onclick with jquery?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<body>
<img id="1" src="" data-price="5">
<img id ="2" src="" data-price="10">
<div id="sum"></div>
</body>
</html>

提前谢谢.

推荐答案

定位图像的ID,并附加一个事件处理程序,然后单击以获取带有data()的data属性.

Target the ID of the images, and attach an event handler, then on click, get the data attribute with data()

$('#1, #2').on('click', function() {
    alert( $(this).data('price') );
});

如果您需要以某种方式在#sum元素中对此进行总结,则应该对此进行具体询问.

If you somehow need to sum this up in the #sum element, you should ask specifically about that.

现在你做了吗?

$('#1, #2').on('click', function() {
    var data1 = parseInt( $('#1').data('price') ,10);
    var data2 = parseInt( $('#2').data('price') ,10);

    $('#sum').text(data1 + data2);
});

这篇关于获取数据价格jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆