2016-11-16 62 views
0

我想調用一個我在對象塊中定義的函數,但只有在選擇字段值被更改時纔會調用它。Javascript「Uncaught TypeError:object is not a function」

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>

Fiddle Demo

+0

在哪一行發生此錯誤?當您在調試器中追蹤代碼時,'studRole'的價值是什麼? – 2016-11-16 06:25:48

+0

很可能是學生對象範圍的一個問題,你是在哪裏定義的? –

+5

您與名爲'student'的HTML元素和名爲'student'的對象有衝突的ID。記住HTML ID放在全局對象上。 – 2016-11-16 06:29:27

回答

1

考慮student是在window範圍,你可以參考它作爲window.student.role()

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="window.student.role()"> 
 
      <option value="user">User</option> 
 
      <option value="Admin" >Admin</option> 
 
     </select> 
 
</form>

+0

爲什麼你需要這樣做? – 2016-11-16 06:26:31

0

你需要調用functionobject你是一個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(); 
} 
0

請確定您函數調用它first.You正在調用哪一個不是在你的腳本中定義的「測試」功能。

<!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> 
相關問題