jQuery .val 更改不會更改輸入值?


為此,您需要使用 attr()。attr() 方法既可用於從匹配集中第一個元素中獲取屬性值,也可將屬性值設定到所有匹配的元素上。

以下是 JavaScript 程式碼 −

示例

 現場演示

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/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+ 瀏覽次數

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告