如何在JavaScript中设置一个表单<input>文本字段的默认值?
当前回答
下面的代码工作得很好:
var $div = ('#js-div-hour input');
$div.attr('value','2022/01/10');
其他回答
我使用setAttribute():
<input type="text" id="example"> // Setup text field
<script type="text/javascript">
document.getElementById("example").setAttribute('value','My default value');
</script>
试试这些。
document.getElementById("current").value = 12
// or
var current = document.getElementById("current");
current.value = 12
简单的答案不是在Javascript中,获得占位符最简单的方法是通过占位符属性
<input type="text" name="text_box_1" placeholder="My Default Value" />
你也可以试试:
document.getElementById('theID').value = 'new value';
直接访问
如果你使用ID,那么你可以直接访问JS全局作用域中的输入
myInput。值= 'default_value' <输入id = " myInput " >
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?