[英]Unable to get the updated value for dataset in div using javascript or jquery
I am working on some java script changes for updating the div data attribute values. 我正在进行一些java脚本更改以更新div数据属性值。 Below is the detailed explanation.
以下是详细说明。
I have a div with some data attributes and initially when the page loads they are set to some default values. 我有一个带有一些数据属性的div,最初当页面加载时,它们被设置为一些默认值。
<div class="offer_option" id="offerView" data-term="24" data-rc="54.99">
<a href="#" class="customize_offer" id="customize">Select</a>
</div>
Through an Ajax call i am trying to update the "data-rc" value as below. 通过Ajax调用,我试图更新“data-rc”值,如下所示。
document.getElementById("offerView").dataset.rc = 100.00;
or 要么
$("#offerView").data('rc', 100.00);
When i click the "select" link the below code will execute 当我单击“选择”链接时,将执行以下代码
$('.customize_offer').unbind('click').click(function() {
selectOffer($(this));
});
Below is the "selectOffer" function 下面是“selectOffer”功能
function selectOffer(selectButton) {
var offerOption = selectButton.parents('.offer_option');
var offerDetails = offerOption.data();
console.log( offerDetails );
}
The problem is the dataset does not showing the updated value for data-rc in the "offerDetails" variable, instead it is showing an empty value. 问题是数据集没有在“offerDetails”变量中显示data-rc的更新值,而是显示一个空值。 I have tried with some j query options and java script options but i am not able to get the updated values in the "offerDetails" variable.
我尝试了一些j查询选项和java脚本选项,但我无法在“offerDetails”变量中获取更新的值。 Please let me know how to fix this.
请让我知道如何解决这个问题。
Thanks, Nagendra 谢谢,Nagendra
Try 尝试
$("#offerView").attr('data-rc', 100.00);
Why it happens? 为什么会这样?
jQuery .data()
is initially populated with values from the data-attributes by using a special cache object inside jQuery to store data .It doesn't change the attribute in the DOM. jQuery .data()
最初使用jQuery中的特殊缓存对象来填充数据属性中的值来存储数据。它不会更改DOM中的属性。 To change the attribute, you have to use attr()
要更改属性,必须使用
attr()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.