我正在尝试调用我在对象块中定义的函数,但只有在更改选择字段值时才会调用。
var student = {
role: function() {
var studRole = document.getElementById("student");
var roleStud = studRole.options[studRole.selectedIndex].value;
switch (roleStud) {
case 'Admin':
alert("Welcome Admin");
break;
default:
}
}
}
<form>
<select id="countries" onchange="test()">
<option value="France">France</option>
<option value="Nigeria">Nigeria</option>
</select>
<br/>Student Role:
<select id="student" onchange="student.role()">
<option value="user">User</option>
<option value="Admin">Admin</option>
</select>
</form>
答案 0 :(得分:1)
考虑user
范围内的ControllerAs
,您可以在student
window
window.student.role()
答案 1 :(得分:0)
您需要使用function
来调用object
,string
<form>
<select id="countries" onchange="test()">
<option value="France">France</option>
<option value="Nigeria" >Nigeria</option>
</select><br/>
Student Role:
<select id="student" onchange="CheckRole()">
<option value="user">User</option>
<option value="Admin" >Admin</option>
</select>
</form>
<script>
var student = {
role: function () {
var studRole = document.getElementById("student");
var roleStud = studRole.options[studRole.selectedIndex].value;
switch (roleStud) {
case 'Admin':
alert("Welcome Admin");
break;
default:
}
}
}
function CheckRole() {
student.role();
}
答案 2 :(得分:0)
请先定义您的功能。您正在调用未在脚本中定义的“测试”功能。
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
height: 50px;
}
</style>
</head>
<body>
<form>
<select id="countries" onchange="test()">
<option value="France">France</option>
<option value="Nigeria" >Nigeria</option>
</select><br/>
Student Role:
<select id="student" onchange="window.student.role()">
<option value="user">User</option>
<option value="Admin" >Admin</option>
</select>
</form>
<script>
function test(){
var student = {
role: function () {
var studRole = document.getElementById("student");
var roleStud = studRole.options[studRole.selectedIndex].value;
switch (roleStud) {
case 'Admin':
alert("Welcome Admin");
break;
default:
}
}
}
}
</script>
</body>
</html>