最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data attribute from radio button with jQuery - Stack Overflow

programmeradmin3浏览0评论

I have the following html convertd to haml:

%input#insurance_isp_payment{ checked: "checked", type: "radio", name: "insurance_isp_payment", price: 27.22, value: "single"}
27,22 €

And now I want to get this price value from radio button which is checked. I tried something like that:

$('input[name=insurance_isp_payment]:checked').data("price")

But this not work. How can I solve this problem?

I have the following html convertd to haml:

%input#insurance_isp_payment{ checked: "checked", type: "radio", name: "insurance_isp_payment", price: 27.22, value: "single"}
27,22 €

And now I want to get this price value from radio button which is checked. I tried something like that:

$('input[name=insurance_isp_payment]:checked').data("price")

But this not work. How can I solve this problem?

Share Improve this question asked Sep 10, 2014 at 9:37 Mateusz UrbańskiMateusz Urbański 7,86216 gold badges74 silver badges141 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 16

Try this : instead of data() use attr() to get the price value.

var price = $('input[name="insurance_isp_payment"]:checked').attr("price");
alert(price);
var price = $('input[name="insurance_isp_payment"]:checked').attr("data-price");
alert(price);

$('input[type=radio][name=insurance_isp_payment]').on('change', function() {

var price = $('input[name="insurance_isp_payment"]:checked').attr("data-price");
alert(price);

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-checked="yes" type="radio" name="insurance_isp_payment" value="100" checked data-price="100" />Single
<input data-checked="no" type="radio" name="insurance_isp_payment" value="200" data-price="200"/>Double

发布评论

评论列表(0)

  1. 暂无评论