开发者

setting textarea value in IE and FF

A demo for sharing data between an input (id=obHid) and a textarea (id=idField)

 if($.browser.msie)
  $("#"+idField).text($("#obHid").val()); // IE
 else
  $("#"+idField).attr("valu开发者_如何学运维e", $("#obHid").val()); // FF

Iskrahelk,


Don't use either of those methods, and especially don't use browser sniffing. Touching $.browser is almost always a mistake.

The proper way to read and write form field values in jQuery is val(). It doesn't matter whether the form field involved is an <input type="text"> or a <textarea>, they'll both work the same.

$('#'+idField).val($("#obHid").val());

[Aside: however this will break if idField may contain dots, as in a selector string they'll turn into class selectors. If that's a concern just use the plain JavaScript version:

document.getElementById(idField).value= document.getElementById('obHid').value;

a bit wordier, but more straightforward.]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜