如何在JavaScript中设置一个表单<input>文本字段的默认值?


当前回答

下面的代码工作得很好:

var $div = ('#js-div-hour input');
$div.attr('value','2022/01/10');

其他回答

这是一种方法:

document.getElementById("nameofid").value = "My value";

你也可以试试:

document.getElementById('theID').value = 'new value';
<input id="a_name" type="text" />

下面是使用jQuery的解决方案:

$(document).ready(function(){
  $('#a_name').val('something');
});

或者,使用JavaScript:

document.getElementById("a_name").value = "Something";

快乐编码:)

<form>
  <input type="number"  id="inputid"  value="2000" />
</form>


<script>
var form_value = document.getElementById("inputid").value;
</script>    

您也可以将默认值更改为新值

<script>
document.getElementById("inputid").value = 4000;     
</script>

如果你使用多种表单,你可以使用:

<form name='myForm'>
    <input type='text' name='name' value=''>
</form>

<script type="text/javascript"> 
    document.forms['myForm']['name'].value = "New value";
</script>