JavaScript alert
JavaScript alert
本文关键字:alert JavaScript 更新时间:2023-09-26
我的家庭作业有问题。。。我不知道我的代码出了什么问题。。我的作业是创建一个简单的学习数学与水平选择。。。我使用下拉菜单来选择级别和算术运算。。。现在我的问题是,当我点击按钮时,它将转到函数start(),然后将所选的级别和操作保存到两个变量中,即levelselect和operationselect。。。现在我测试它的警报(levelselect)。。。假设它会显示我选择的级别的警报窗口。。。但它不起作用。。。感谢查看我的帖子并愿意帮助我…谢谢…><
<script language="javascript">
var levelselect;
var operationselect;
var num1;
var num2;
function start()
{
levelselect = form.element["form1"]["level"].value;
operationselect = form.element["form1"]["operation"].value;
alert(levelselect);
}
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table align="center">
<tr align="center"><td><label>Choose your Level :</label>
<select id="level" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</td>
</tr>
<tr align="center"><br><br><br><td><label>Choose your arithmetic operations :</label>
<select id="operation" >
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
<option value="div">Division</option>
<option value="mul">Multiplication</option>
</select>
</td>
</tr>
<tr>
<td align="center"><input type="button" onclick="start()" value="Generate question"/></td>
</tr>
</table>
</form>
</body>
</html>
如果您想将表单用作javascript变量,您应该更改输入类型并删除函数调用:
<input type="submit" value="Generate question" />
然后更改:
<form id="form1" name="form1" method="post" action="" onsubmit="start(this);return false;">
通过这种方式,函数将在点击输入时调用,但我们传递的是:
function start(myForm) {
levelselect = myForm["level"]value;
operationselect = myForm["operation"]value;
alert(levelselect);
}
在函数"start"中,我们有一个变量:myForm,可以用来选择的两个选择值
http://jsfiddle.net/L64d5nLe/1/
从您的演示中删除Script标签,并使用ID获取元素,以解决您的问题
var levelselect;
var operationselect;
var num1;
var num2;
function start()
{
levelselect = document.getElementById("level").value;
operationselect = document.getElementById("operation").value;
alert(levelselect);
alert(operationselect)
}
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table align="center">
<tr align="center"><td><label>Choose your Level :</label>
<select id="level" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</td>
</tr>
<tr align="center"><br><br><br><td><label>Choose your arithmetic operations :</label>
<select id="operation" >
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
<option value="div">Division</option>
<option value="mul">Multiplication</option>
</select>
</td>
</tr>
<tr>
<td align="center"><input type="button" onclick="start()" value="Generate question"/></td>
</tr>
</table>
</form>
</body>
</html>
您可以访问这样的表单元素。
levelselect = document.forms["form1"]["level"].value
选中此项,您可以使用id获取选择值,如:
document.getElementById("operation").value;
这将为您返回选定的值。您不需要使用表单名称进行检查。我还将type=button改为button标签。
var levelselect;
var operationselect;
function start()
{
levelselect = document.getElementById("level").value;
operationselect = document.getElementById("operation").value;
alert(levelselect+" ## "+operationselect);
}
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table align="center">
<tr align="center"><td><label>Choose your Level :</label>
<select id="level" >
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
<tr align="center"><br><br><br><td><label>Choose your arithmetic operations :</label>
<select id="operation" >
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
</select>
</td>
</tr>
<tr>
<td align="center"><button onclick="start()" >Generate question</button></td>
</tr>
</table>
</form>
</body>
</html>
请在启动函数中进行这些更改
levelselect = document.getElementById('level').value;
operationselect = document.getElementById('operation').value;
我从未在任何地方见过这个form.element。
您应该使用document.getElementById(id)获取页面的一个元素以获得值:
function start()
{
var a = document.getElementById('level');
var strLevel = a.options[a.selectedIndex].value;
var b = document.getElementById('level');
var strOperation = b.options[b.selectedIndex].value;
alert(strLevel);
}
基本上你是:
- 在"id"的帮助下选择"select"
- 您正在查看所选选项
- 您将获得所选值
我用这个脚本为您制作了一个jsfiddle:
http://jsfiddle.net/0x4ex8tk/6/
相关文章:
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- Javascript仅适用于alert()和Debug模式
- Javascript alert()在没有const字符串的情况下不显示
- C# WebView 不理解 JavaScript 调用 alert()(Windows 8 XAML 应用程序)
- Javascript表单if have alert停止重定向save.php
- 为什么不应该't我在JavaScript中使用Alert
- 如果我在其中使用alert,Javascript代码就可以工作,否则它'It’不起作用
- 我如何模仿Javascript中的alert()功能来阻止页面
- 自定义Javascript Alert Popup onclick svg路径
- Javascript问题,脚本只能100%使用alert();到位ODD
- 在我执行alert()之前,Javascript array.length为0
- Javascript alert() 在弹出窗口中
- 在 Javascript 中使用警报时出错(对象的属性“alert”不是函数)
- JavaScript 中的 alert(status) 总是导致 null
- echo php javascript alert?
- Alert innerHTML of element with javascript
- Asp.Net Page JavaScript alert 命令可更改程序行为
- 在 JavaScript alert() 中打印 EL 字符串 “RP” 会导致 JS 错误“'RP'
- view php msg in alert javascript
- 如果变量为真,DON'T SHOW alert?Javascript