保存文本在txtfield中,不起作用?

时间:2015-12-09 15:27:52

标签: javascript html css html5

如何在文本框中保存文字?

当我重新加载页面时,该文本框将被删除。

我试过这个。

<td align='left' bgcolor='#cfcfcf'><input type="text" name="txtfield" value="" placeholder="input your text" localstorage.setitem(name) /></td>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

<html>
<body>


<td align='left' bgcolor='#cfcfcf'>
<input type="text" name="txtfield" id="txtfield" value="" 
placeholder="input your text"  onchange="storeitem(this.value)"/>
</td>

<script language="javascript">
function storeitem(val){
    localStorage.setItem('txtfield', val)
}

function loaditem(){

    var txtfield = document.getElementById('txtfield');
    txtfield.value = localStorage.getItem('txtfield');

}

window.onload = loaditem;
</script>
</body>

</html>