Javascript set value of an input field

2019-04-08 06:26发布

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.

3条回答
啃猪蹄的小仙女
2楼-- · 2019-04-08 06:51

So using the following method for setting attributes worked fine.

document.getElementById("geofeld").setAttribute("value", geo_poi);
查看更多
干净又极端
3楼-- · 2019-04-08 07:06

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

查看更多
一纸荒年 Trace。
4楼-- · 2019-04-08 07:14

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

document.getElementById("geofeld").setRangeText("text");
查看更多
登录 后发表回答