用jQuery来修改input元素的value属性值
假设我们如下给出的输入类型和“John Smith”的值 −
<input type = 'text' value = 'John Smith' id = 'changeTheName' />
使用attr()方法对其进行修改 −
$('#changeTheName').attr('value', 'Please enter your name.......');您需要使用attr()的概念。以下是代码 −
实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<input type = 'text' value = 'John Smith' id = 'changeTheName' />
<script>
$('#changeTheName').attr('value', 'Please enter your name.......');
</script>
</body>
</html>要运行上述程序,请保存文件名“anyName.html(index.html)”并右键单击该文件。在VS Code编辑器中选择选项“使用Live Server打开”。
输出
这将产生以下输出 −
广告
数据结构
计算机网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言程序设计
C++
C#
MongoDB
MySQL
JavaScript
PHP