有人可以幫忙嗎?我已經為此苦苦掙扎了好幾個小時。這是代碼。我必須能夠在框中輸入任何名稱單擊顯示按鈕以顯示我輸入的內容,與你好連接并說出我在下面輸入的內容然后還能夠點擊清除按鈕以清除所有內容,就像重繪 一樣我也有在我的另一個頁面上呼叫一個名為 display Hello Message 的函式。我只能讓它顯示你好和名字并清除一件事。這就是我所擁有的。
<!DOCTYPE html>
<html>
\
<head>
<title>JavaScript: Input & Output Assignment</title>
</head>
<body>
<h1>Hello Name - Input & Output</h1>
<div>
<label for="firstNameInput"> First Name</label>
<input type="text" id="firstNameInput" placeholder="Enter a first name" />
<label for="lastNameInput"> Last Name</label>
<input type="text" id="lastNameInput" placeholder="Enter a last name" />
<button onclick="displayHelloMessage()">Display</button> <button onclick="displayHelloMessage()">Clear</button>
</div>
<p id="helloNameOutput"></p>
<script src="/script.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
function displayHelloMessage() {
var fname = document.getElementById("firstNameInput").value
var lname = document.getElementById("lastNameInput").value
document.getElementById("helloNameOutput").innerHTML = "Hello " fname " " lname
}
function clearMessage() {
document.getElementById("helloNameOutput").innerHTML = ""
}
將這些函式添加到您的 js 檔案中并將 onClick 清除按鈕更改為 clearMessage
uj5u.com熱心網友回復:
由于我不知道您的 script.js 中發生了什么,我在 HTML-script-tag 中放置了一些自定義 jQuery,并洗掉了按鈕之一的 onclick 功能。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
\
<head>
<title>JavaScript: Input & Output Assignment</title>
</head>
<body>
<h1>Hello Name - Input & Output</h1>
<div>
<label for="firstNameInput"> First Name</label>
<input type="text" id="firstNameInput" placeholder="Enter a first name" />
<label for="lastNameInput"> Last Name</label>
<input type="text" id="lastNameInput" placeholder="Enter a last name" />
<button class="display">Display</button> <button class="clear">Clear</button>
</div>
<p id="helloNameOutput"></p>
<script>
$('.display').on('click', function() {
var firstname = $('#firstNameInput').val();
var lastname = $('#lastNameInput').val();
$('#helloNameOutput').text('Hello ' firstname ' ' lastname) })
$('.clear').on('click', function() {
$('#firstNameInput').val('');
$('#lastNameInput').val('');
$('#helloNameOutput').text('') })
</script>
</body>
</html>
uj5u.com熱心網友回復:
使輸入的值無效。例如,如果清除按鈕觸發了 onclick 事件,則呼叫的函式可以通過執行以下操作使輸入無效:
document.getElementById("firstNameInput").value = ""; document.getElementById("lastNameInput").value = "";
一切都是通過純 Javascript 完成的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522059.html
上一篇:文本Quarto中的下載按鈕
