我正在嘗試(沒有運氣)Swal在 If 陳述句中實作一個內部。
這是我所做的:
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("numero");
//check if try to copy with the empty input
if(document.getElementById("numero").value == ""){
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Nothing to copy!'
})
// alert("Nothing to copy!!")
} else {
}
這是我的html中的鏈接:
<!-- dark theme for swal -->
<link href="//cdn.jsdelivr.net/npm/@sweetalert2/theme-dark@4/dark.css" rel="stylesheet">
<!-- javascript file -->
<script src="app.js"></script>
<!-- swal link -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
關于如何使這項作業的任何提示?我想我的問題在 If 陳述句中,但我不知道如何解決它
uj5u.com熱心網友回復:
在您的代碼中,我沒有看到事件偵聽器。用于keyup驗證元素的值是否為空。
var copyText = document.getElementById("numero");
copyText.addEventListener('keyup', () => {
if (document.getElementById("numero").value == "") {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Nothing to copy!'
})
}
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<head>
<link href="//cdn.jsdelivr.net/npm/@sweetalert2/theme-dark@4/dark.css" rel="stylesheet">
<script src="app.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
</head>
<body>
<input type="text" id="numero">
</body>
</html>
uj5u.com熱心網友回復:
最終一切都很好,但是......我忘記把它放在我的代碼中
event.preventDefault()
這可以防止 Swal 無法正常作業。
到最后,最終的代碼是這樣的:
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("numero");
if(document.getElementById("numero").value == ""){
Swal.fire(
'Ooops...',
'Nothing to copy here!',
'error'
)
event.preventDefault()
} else {}
/*code here
}
謝謝你們給我這些建議??
uj5u.com熱心網友回復:
首先,確保您的匯入是正確的。您可以嘗試使用簡單的方法進行除錯,例如:swal("Hello world")代碼中的任何位置。我大致復制了你的,并使用了來自他們網站上的 Swal 檔案的不同 CDN 匯入:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
在您的 Javascript 代碼中,您在}函式末尾缺少 a 。也可能是一個錯誤的復制,但永遠不要太確定。
此外,如果您想確保您的 if 陳述句是正確的,您可以嘗試console.log("Hello world")在代碼中的每個 fork 處進行除錯。然后,在瀏覽器中打開控制臺以查看記錄的內容。這將幫助您確定您的代碼是否在您想要的時候運行。
另外,使用時要小心document.getElementById("numero").value。例如,您的代碼無法使用,<p>因為<p>沒有價值,但它可以使用<input>or <option>。
這是我正在使用的代碼示例:
function myFunction() {
var copyText = document.getElementById("numero");
if (document.getElementById("numero").innerHTML == "") {
swal({
icon: 'error',
title: 'Oops...',
text: 'Nothing to copy!'
})
} else {
swal("Copied!")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454877.html
標籤:javascript html if 语句 警报
