I am working on some java script changes for updating the div data attribute values. Below is the detailed explanation.
我正在修改一些java脚本以更新div数据属性值。下面是详细的解释。
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. 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. Please let me know how to fix this.
问题是,数据集没有在“offerDetails”变量中显示数据rc的更新值,而是显示一个空值。我尝试了一些j查询选项和java脚本选项,但是我无法获得“offerDetails”变量中的更新值。请告诉我如何解决这个问题。
Thanks, Nagendra
谢谢,Nagendra
1 个解决方案
#1
3
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. To change the attribute, you have to use attr()
jQuery .data()最初是通过使用jQuery内部的一个特殊缓存对象来存储数据来填充数据属性的,它不会改变DOM中的属性。要更改属性,必须使用attr()
#1
3
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. To change the attribute, you have to use attr()
jQuery .data()最初是通过使用jQuery内部的一个特殊缓存对象来存储数据来填充数据属性的,它不会改变DOM中的属性。要更改属性,必须使用attr()