最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Which value will be sent by <textarea> and why? - Stack Overflow

programmeradmin3浏览0评论
<form>
<textarea name="test" value="no">
hi
</textarea>
<input type="submit" />
</form>

hi or no,and reason?

I ask this question because I see javascript editors use textarea.value = html; to restore the content when submit,why they do this if value attribute is useless?

<form>
<textarea name="test" value="no">
hi
</textarea>
<input type="submit" />
</form>

hi or no,and reason?

I ask this question because I see javascript editors use textarea.value = html; to restore the content when submit,why they do this if value attribute is useless?

Share Improve this question edited Dec 11, 2009 at 4:59 user198729 asked Dec 11, 2009 at 4:08 user198729user198729 63.6k109 gold badges255 silver badges352 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 18

hi will be submitted. There is no value attribute for the <textarea> tag. See W3School's textarea tag reference for details.

To answer your question of why you see javascript libraries accessing the value property of a textarea DOM element, you have to appreciate that HTML and the DOM (Document Object Model) that javascript accesses are 2 different animals. In HTML the value of a <textarea> is its contents. In DOM, the value of a textarea node is contained in its value property. Although DOM property names often map 1:1 to HTML attribute names, they don't always and this is just one example.

In a POST response, a textarea will respond with the contents of innerHTML. However, if you have set a value attribute to the textarea and you try getting textarea.value in JavaScript, you will receive the contents of the value attribute.

A browser should never display the value attribute of a textarea in the physical textarea because it isn't standard. The textarea tag operates differently from the input tags. I would assume it does that because the input tag doesn't support line breaks.

The value a user changes would be the innerHTML value (html in jQuery), not the value attribute like they do with input fields.

A couple other points -

All browsers that support javascript return read/write values for textarea.value, consistent with the input elements. But you can't use getAttribute('value') or setAttribute('value') with a textarea.

You can also read a textarea's 'type' property,though type is not an attribute either.

If you set the value property, textarea.value=string;**it is equivilent to **textarea.appendChild(document.createTextNode(string))-

A textarea can not contain any other elements, only text nodes.

The text in the textarea set from value will be the literal characters from the string, while setting the innerHTML property will escape any entities and minimize white-space.

The value of a textarea is it's contents. The 'value' property does not have a standard meaning for this tag, so a form or JavaScript will get 'hi' from the textarea. Here's a demonstration of this in JavaScript.

<form> 
 <textarea name="test" value="no" id='ff'>
  hi
 </textarea>
<input type="submit" />
</form>
<br>
<a onclick='showVal("ff")'>Show Value</a>
<script>
 function showVal(id){
   var snib=document.getElementById(id);
   alert(snib.value);
   }
</script>
发布评论

评论列表(0)

  1. 暂无评论