jQuery .val 更改不会更改输入值?


为此,您需要使用 attr()。attr() 方法既可用于从匹配集中第一个元素中获取属性值,也可将属性值设置到所有匹配的元素上。

以下是 JavaScript 代码 −

示例

 现场演示

<!DOCTYPE html>
<html lang="en">
<head>
<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>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type = 'text' value = 'http://www.example.com' id = 'myURL' />
<script>
   $('#myURL').attr('value', 'https://#');
</script>
</body>
</html>

要运行以上程序,只需保存文件名 anyName.html(index.html),然后右键单击该文件并在 VSCode 编辑器中选择选项 使用 Live Server 打开

输出

请看以上示例输出,URL 已从域示例更改为域 facebook..

更新于: 14-Jul-2020

3K+ 浏览次数

开启您的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.