简体   繁体   English

弹出警告用户在文本框中写了什么

[英]Pop up alert What the user have written in a textbox

I have an exercise for my University and i'm stuck.我的大学有一个练习,但我被卡住了。 I want to make a page which by pressing the "Search" button will display a pop-up (alert) with the content of the textbox containing the "Student Id" , while pressing the "Insert" button will display a pop-up (alert) with the contents of the textbox containing "First Name".我想制作一个页面,通过按“搜索”按钮将显示一个弹出窗口(警报),其中包含包含“学生 ID”的文本框的内容,而按“插入”按钮将显示一个弹出窗口(警报)与包含“名字”的文本框的内容。

Here is my code :这是我的代码:

<html>
<body>
<form action="/action_page.php">
<label for="studentid">Student Id:</laber><br>
<input type="text" id="studentid" name="studentid"><br>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<button type="button">Search</button>
<button type="button">Insert</button> 
</form>
</body>
</html>

Edit your HTML to include this on the buttons:编辑您的 HTML 以将其包含在按钮上:

<button type="button" onclick="search()">Search</button>
<button type="button" onclick="insert()">Insert</button>

and add this to the end:并将其添加到最后:

<script>
function search() {
    let studentID = document.querySelector("#studentid").value;
    alert(studentID);
}
function insert() {
    let firstName = document.querySelector("#fname").value;
    alert(firstName);
}
</script>

This HTML code will run the javascript functions " search() " or " insert() " on click respectively, which do the following:此 HTML 代码将分别在单击时运行 javascript 函数“ search() ”或“ insert() ”,它们执行以下操作:

  1. Create a variable, and assign the value of either the " studentid " or " fname " elements to it.创建一个变量,并将“ studentid ”或“ fname ”元素的值分配给它。
  2. Alert() the contents of the variable. Alert() 变量的内容。

The onclick attribute runs javascript assigned to it when the element it is on is clicked.当单击它所在的元素时, onclick属性运行分配给它的 javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM