强制attr更新后,JQuery html()调用不返回值

时间:2022-09-06 15:36:13

Maybe I'm not understanding something basic here, but the html() call doesn't return an element with all attributes filled in, even after an explicit call to set the attribute.

也许我不理解这里的基本内容,但是html()调用不返回填充了所有属性的元素,即使在显式调用设置属性之后也是如此。

For example, I have a text box:

例如,我有一个文本框:

<input type="text" id="foo" value="" />

When the user fills in a value in this box, a function is called (onBlur of input text box).

当用户在此框中填写值时,将调用一个函数(输入文本框的onBlur)。

$('#foo').blur(function(event) { updateFoo(this); });

In that function the following code exists:

在该函数中,存在以下代码:

function updateFoo(input) {
  // force DOM update??
  $('#'+input.id).attr('value', input.value);

  // successfully displays value user entered
  alert($('#'+input.id).attr('value')); 

  // displays input element, with value not filled in,
  // like: 'input type="text" id="foo" value="" />'
  alert($('#'+input.id).parent().html()); 

  ...
}

Shouldn't the html() call return the element with the value attribute set?

不应该html()调用返回值属性设置的元素吗?

I'm using Firefox 3.6.13 on Max OSX.

我在Max OSX上使用Firefox 3.6.13。

I saw this forum post: http://forums.asp.net/t/1578929.aspx which I based some of my assumptions off of...

我看到这个论坛帖子:http://forums.asp.net/t/1578929.aspx我根据我的一些假设...

Thanks, Galen

2 个解决方案

#1


4  

After researching some more, I ran across this post:

在研究了一些之后,我遇到了这篇文章:

jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

Firefox中的jQuery html()(使用.innerHTML)忽略DOM更改

which pretty much sums up the problem I'm having. Basically it's a firefox implementation issue...

这几乎总结了我遇到的问题。基本上它是一个firefox实现问题......

Instead of:

$('#'+input.id).attr('value', input.value);

I'm now using:

我现在正在使用:

input.setAttribute('value', input.value); // force DOM update

which solved the problem.

这解决了这个问题。

#2


2  

I think this is a side effect of the fact that value has a very specialized use. I imagine that the value is stored in some local property instead of in the DOM itself.

我认为这是价值具有非常专业用途这一事实的副作用。我想这个值存储在一些本地属性中,而不是存储在DOM本身中。

If you change the attr to something else, like newattr, it works fine:

如果你将attr更改为其他东西,比如newattr,它可以正常工作:

http://jsfiddle.net/m9vWT/

Of course, that doesn't help with the value attribute...

当然,这对价值属性没有帮助......

#1


4  

After researching some more, I ran across this post:

在研究了一些之后,我遇到了这篇文章:

jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

Firefox中的jQuery html()(使用.innerHTML)忽略DOM更改

which pretty much sums up the problem I'm having. Basically it's a firefox implementation issue...

这几乎总结了我遇到的问题。基本上它是一个firefox实现问题......

Instead of:

$('#'+input.id).attr('value', input.value);

I'm now using:

我现在正在使用:

input.setAttribute('value', input.value); // force DOM update

which solved the problem.

这解决了这个问题。

#2


2  

I think this is a side effect of the fact that value has a very specialized use. I imagine that the value is stored in some local property instead of in the DOM itself.

我认为这是价值具有非常专业用途这一事实的副作用。我想这个值存储在一些本地属性中,而不是存储在DOM本身中。

If you change the attr to something else, like newattr, it works fine:

如果你将attr更改为其他东西,比如newattr,它可以正常工作:

http://jsfiddle.net/m9vWT/

Of course, that doesn't help with the value attribute...

当然,这对价值属性没有帮助......