我遇到的问题是,当我尝试通过数据键更新文本元素时,它给了我"对象不支持属性或方法'数据'& #34;错误。
我之前在 MealNameError 函数中使用过这个来做同样的事情,但是当我在 MealItemError 函数上尝试它时,我遇到了问题。
function MealNameError(databaseMealName) {
var $this = $("#ContentPlaceHolder1_AddMealName");
$('#' + $this.data('describeby')).text('"' + databaseMealName + '"
already exists').show();
}
function MealItemError(duplicateItems) {
$('#addItemDropdownList_1, #addItemDropdownList_2, #addItemDropdownList_3, #addItemDropdownList_4, #addItemDropdownList_5')
.each(function() {
for (var i = 0; i < duplicateItems.length; i++) {
if (parseInt($(this).val()) === duplicateItems[i]) {
$('#' + this.data('describeby')).text('Only one of same Item').show(); //This line is throwing the error
}
}
});
}
我通常会输入数据(&#39;键&#39;,值&#39;),但价值是动态的,具体取决于他们添加的食品数量。我对这两个问题的看法有何不同?
答案 0 :(得分:1)
您有两种方法可以解决该问题:
this
$
上下文
$('#' + $(this).data('describeby'))
dataset
属性。$('#' + this.dataset.describeby)