[英]data attribute doesn't change when variable value is changed
THIS IS NOT A DUPLICATE BECAUSE I USE .data()
and NOT .attr()
LIKE ATTACHED SUGGESTS 这不是一个重复,因为我使用
.data()
而不是.attr()
喜欢附加的建议
I need to change a data attribute value when a variable is updated. 我需要在更新变量时更改数据属性值。 I'm making a dropdown that ties to a discount field and I need to update both the text and the data attribute that is in the
<option>
tag. 我正在制作一个与折扣字段相关的下拉列表,我需要更新
<option>
标记中的文本和数据属性。
The relevant pieces looks like this: 相关部分如下所示:
$(function() {
$("#plan-options").change(function() {
var moneyAmount = $(this).find('option:selected').data('amount');
$("#selected-price").text("$" + moneyAmount);
$("#purchase-warning").toggle();
$(".default-encouragement").toggle();
});
});
#plan-options
is a <select>
tag with <option>
s that have data-attributes. #plan-options
是一个带有<option>
的<select>
标签,它具有数据属性。
after that: 之后:
...
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let amountOffMoney = (data.amount_off / 100).toFixed(2);
if (data.percent_off != null) {
selectedAmount = selectedAmount * (100 - data.percent_off) / 100
} else if (data.amount_off != null) {
selectedAmount = selectedAmount - amountOffMoney;
// this variable doesn't update - why?
$("select option:selected").data("amount", selectedAmount);
} else {
alert("Someting wrong happened!");
return;
}
The most relevant piece is here: 最相关的部分在这里:
selectedAmount = selectedAmount - amountOffMoney;
$("select option:selected").data("amount", selectedAmount);
My expectation is that I assign selectedAmount
a new value, and changing the data attribute to selectedAmount
should change to the new assignment but that is not happening. 我的期望是我为
selectedAmount
分配了一个新值,并且将data属性更改为selectedAmount
应该更改为新的赋值,但这不会发生。 The value stays the same. 值保持不变。
Is it because it's let
? 是因为它
let
? Is it a scoping issue? 这是一个范围问题吗?
FULL CODE SNIPPET: 完整的代码:
$(function() {
$("#plan-options").change(function() {
var moneyAmount = $(this).find('option:selected').data('amount');
$("#selected-price").text("$" + moneyAmount);
$("#purchase-warning").toggle();
$(".default-encouragement").toggle();
});
});
...
jQuery(function ($) {
$(document).on("click",'.apply_coupon', function (e) {
e.preventDefault();
let plan = $('select[name="plan"]').val();
let coupon = $('input[name="coupon"]').val();
$.get('/premium/coupon/', {
coupon: coupon,
plan: plan
}, function (data) {
console.log("got data from calling coupon api:", data)
if (data.success) {
//discounted amount display handling
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let amountOffMoney = (data.amount_off / 100).toFixed(2);
if (data.percent_off != null) {
selectedAmount = selectedAmount * (100 - data.percent_off) / 100
} else if (data.amount_off != null) {
selectedAmount = selectedAmount - amountOffMoney;
console.log(selectedAmount);
$("#plan-options option:selected").data("amount", selectedAmount);
} else {
alert("Someting wrong happened!");
return;
}
let regex = /\d+\.*\d*/;
let newStr = selectedStr.replace(regex, selectedAmount.toString());
selected.text(newStr);
$('.apply_coupon').text("Remove");
$('.apply_coupon').addClass("remove_coupon").removeClass('apply_coupon');
$('input[name="couponVerified"]').val(1);
$('input[name="coupon"]').hide();
$('.coupon-results').show();
$('.coupon-results__code').text(data.name);
$('.coupon-results__info').text("$" + amountOffMoney + " off " + data.duration);
$("#selected-price").text("$" + selectedAmount);
} else {
alert(data.message);
}
})
});
$(document).on('click','.remove_coupon', function (e) {
e.preventDefault();
$.get('/premium/coupon/remove/', function (data) {
if (data.success) {
//discounted amount reverting handling
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let regex = /\d+\.*\d*/;
let newStr = selectedStr.replace(regex, selectedAmount.toString());
selected.text(newStr);
$('.remove_coupon').text("apply");
$('.remove_coupon').addClass("apply_coupon").removeClass('remove_coupon');
$('input[name="couponVerified"]').val(0);
$('input[name="coupon"]').show();
$('.coupon-results').hide();
$("#selected-price").text("$" + selectedAmount);
}
});
});
});
It looks like you are specifying variables data.amount_off
and data.percent_off
that are undefined
? 看起来您正在指定
undefined
变量data.amount_off
和data.percent_off
? I'm assuming these are also supposed to be data
attributes on the <option>
tag? 我假设这些也应该是
<option>
标签上的data
属性? The below snippet calls a test function on change of the select
box value, to trigger your value calculations using the data
attributes. 下面的代码片段在更改
select
框值时调用测试函数,以使用data
属性触发您的值计算。 Please let me know if you have any questions. 请让我知道,如果你有任何问题。 Note that switching from the $15.00
option
back to the default "Select" option
and then back to the $15.00 option
will update the data-amount
value each time. 请注意,从$ 15.00
option
切换回默认的“Select” option
,然后再返回$ 15.00 option
每次都会更新data-amount
量值。
$(function() { $("#plan-options").change(function() { var moneyAmount = $(this).find('option:selected').data('amount'); $("#selected-price").text(""); $("#purchase-warning").toggle(); $(".default-encouragement").toggle(); // call code function below for testing if (moneyAmount > 0) { // Amount data attribute value on select console.log('current data-amount value: ' + moneyAmount); $("#selected-price").text("$" + moneyAmount); testDataAttr(); } }); }); function testDataAttr() { let selected = $("#plan-options option:selected"); let selectedAmount = selected.data("amount"); let selectedAmountOff = selected.data("amount-off"); let selectedPercentOff = selected.data("percent-off"); let selectedStr = selected.text(); let amountOffMoney = (selectedAmountOff / 100).toFixed(2); if (selectedPercentOff > 0) { selectedAmount = (selectedAmount * (100 - selectedPercentOff) / 100).toFixed(2); } else if (selectedAmountOff > 0) { selectedAmount = (selectedAmount - amountOffMoney).toFixed(2); $("#plan-options option:selected").data("amount", selectedAmount); // Log updated amount that was set on data-amount attr console.log('updated data-amount value: ' + selected.data("amount")); } else { alert("Someting wrong happened!"); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="plan-options"> <option value="">-Select-</option> <option value="" data-amount="15.00" data-amount-off="5.00" data-percent-off="">15.00</option> </select> <div id="selected-price"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.