jQuery数据值未更新 [英] jQuery data value not updating

查看:68
本文介绍了jQuery数据值未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么这不起作用吗?

Could someone explain why this isn't working?

我想做的是使用已放入的新节更新放置元素中的名为"section_id"的数据属性.我在删除数据时会检查此数据属性,以使他们无法将其放入已有的部分中.所以自然地,如果他们将其放入新的部分中,则我希望更新该部分的ID.

What I'm trying to do is update a data attribute called 'section_id' in a dropped element with the new section it has been dropped into. I check this data attribute when dropping so that they aren't able to drop it into the section it's already in. So naturally, if they drop it in a new section, I want that section id updated.

我有一个像这样的div:

I have a div like so:

<div class="draggable" data-section_id="413">...</div>

我正在跟踪值的运行方式:

I'm tracking what the value is doing like so:

alert(newTaskRow.find('.draggable').data('section_id')); //returns 413
newTaskRow.find('.draggable').data('section_id','999');
alert(newTaskRow.find('.draggable').data('section_id')); //returns 999
alert(newTaskRow.find('.draggable').html()); 

但是HTML仍然显示:

But the HTML still shows:

<div class="draggable" data-section_id="413">...</div>

为什么在HTML中不更新值?那我该怎么办?

Why isn't the value being updated in the HTML? And how would I do that?

推荐答案

jQuery的data()不更新属性,它设置内部数据对象,但仅将数据属性用作默认值

jQuery's data() does not update the attributes, it sets an internal data object, but uses the data attribute as the default value only

如果由于某些原因必须更新HTML5数据属性,请使用attr()

If for some reason you have to update the HTML5 data attribute, use attr()

.attr('data-section_id','999')

这篇关于jQuery数据值未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆