Javascript set value of an input field

孤人 提交于 2020-01-01 09:14:07

问题


Since while I cannot set the value of an input field with type=text. Before, I always used something like this:

<input style="display: none" type="text" name="geo_poi" value="" id="geofeld" />

Then, in JavaScript i added code containing a line like this:

document.getElementById("geofeld").value = geo_poi;

This always worked. Maybe the new browsers don't want to support the method above anymore.


回答1:


So using the following method for setting attributes worked fine.

document.getElementById("geofeld").setAttribute("value", geo_poi);



回答2:


For situations when setAttribute does not yield any result (think HTML5 user/password input), consider this:

document.getElementById("geofeld").setRangeText("text");



回答3:


HTML


<input type="text" placeholder="Item 1" id="item1" />
<input type="text" placeholder="Item 2" id="item2" />


JAVASCRIPT


$('#item1').blur(function() {
    var item1var = $('#item1').val();
    $('#item2').val(item1var);
});

Jsfiddle HERE



来源:https://stackoverflow.com/questions/16101999/javascript-set-value-of-an-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!