0

I have this code:

<div id='html'><input type='text' value='' /></div><br><a onClick="alert(document.getElementById('html').innerHTML);">click me</a>

http://jsfiddle.net/EQ88q/

If you change input value and click on "click me" you won't see the real input value. Is there any propety instead of "innerHTML" that gets the real values?

PS: I'm using plain javascript I can't use any library.

4

2 回答 2

1

这将起作用,但它非常粗糙:

<div id='html'>
<input type='text' value=''/>
</div>
<script>
function LinkClick()
{
 var ele = document.getElementById('html');
 ele.children[0].setAttribute("value",ele.children[0].value);
 alert(ele.innerHTML);
}
</script>
<br><a onClick="LinkClick()">click me</a>

我看到 ComCrude 刚刚在评论中提供了这个确切的答案

于 2013-11-12T20:04:12.390 回答
0

elem.innerHTML是元素内部的 HTML 代码。在这种情况下,innerHTML是空字符串,因为标签内没有内容。

您应该使用input_elem.value来获取value输入元素的属性。

于 2013-11-12T19:59:27.490 回答