4 回答

TA貢獻1829條經(jīng)驗 獲得超7個贊
我不確定您想要什么,但我將向您展示如何完全按照您的要求制作警報窗口。首先,你必須考慮你所犯的幾個錯誤。JavaScript 無法識別 Function 這個詞,因為它是大寫的。函數(shù)關(guān)鍵字必須小寫。
最后,要實現(xiàn)您想要的效果,您需要使用事件,尤其是單擊事件。
let button = document.querySelector('#sec1-btn1');
? ??
? ? button.addEventListener('click', function(e) {
? ? ? ? let val = document.querySelector('#sec1-input').value;
? ? ? ? alert(val);
? ? });
? ? <form>
? ? ? <div class="form-group">
? ? ? ? <label for="sec1-input"><strong>Enter Alert Text: </strong></label>
? ? ? ? <input type="text" class="form-control" id="sec1-input" />
? ? ? </div>
? ? ? <button id="sec1-btn1" type="button" class="btn btn-primary">
? ? ? ? Alert Me!
? ? ? </button>
? ? </form>

TA貢獻1921條經(jīng)驗 獲得超9個贊
您還沒有在任何地方調(diào)用該函數(shù)。為了讓它工作,你需要使用一個監(jiān)聽器。
<div class="form-group">
<label for="sec1-input"><strong>Enter Alert Text: </strong></label>
<input type="text" class="form-control" id="sec1-input">
</div>
<button onclick="myfunction1()" id="sec1-btn1" type="button" class="btn btn-primary">Alert Me!</button>
<script>
function myfunction1() {
let myfun1 = document.getElementById('sec1-input').value;
alert(myfun1)
}
</script>
我添加了onClick監(jiān)聽器button,現(xiàn)在它可以工作了。

TA貢獻1776條經(jīng)驗 獲得超12個贊
你不應該用大寫字母來啟動像alert這樣的javascript函數(shù)。
將這段代碼代替您的按鈕:
<button id="sec1-btn1" type="button" class="btn btn-primary" onclick="myfunction1()">Alert Me!</button>

TA貢獻1804條經(jīng)驗 獲得超8個贊
javaScript 區(qū)分大小寫
function myfunction1()
{
let myfun1 = document.getElementById('sec1-input').value;
alert(myfun1);
}
<div class="form-group">
<label for="sec1-label"><strong>Enter Alert Text: </strong></label>
<input type="text" class="form-control" id="sec1-input">
</div>
<button id="sec1-btn1" type="button" onClick="myfunction1()" class="btn btn-primary">Alert Me!</button>
另外元素的 ID 不應該相同,要分配相同的選擇器,使用類,并且您還需要將函數(shù)提供給元素的事件偵聽器
- 4 回答
- 0 關(guān)注
- 217 瀏覽
添加回答
舉報