在我的 JS 檔案中,我有一個函式,其中我使用了兩個變數,choice 和 courseChosen。后者需要先轉換為物件。我的 html 標簽分別是 courseName 和 courseInfo 用于選擇和 courseChosen。這些共享 this.value 由兩者共享(它們應該回傳相同的“值”),并且 this.value 還參考 html 檔案中的一個 id(id=course)。
參考 courseChosen 的 Document.getElementById() 回傳“未定義”作為值,而另一個回傳預期/選擇的值。
HTML:(...)
\<div\>
\<select id="course" multiple size="3" onchange="choiceMade(this.value);"\>
\<select id="course" multiple size="3" onchange="choiceMade(this.value);"\>
\<option value="" selected\>--Choose--\</option\>
\<option value="angular"\>Angular\</option\>
\<option value="reactjs"\>React\</option\>
\<option value="vuejs"\>Vue\</option\>
\<option value="JavaScript"\>JavaScript\</option\>
\<option value="Python"\>Python\</option\>
</select>
</div>
<div> <p>
<div id="courseInfo"> </div>
</p></div>
<div id="favorites">
<div>
<label class="standoutLabel">Favorite Instructor:</label>
</div>
<div>
<label class="standoutLabel">Favorite Instructor:</label>
<input type="text" id="courseName" pattern="[A-Za-z]{1,}[ ]{0,2}">
</div>
(...)
JS:
(...)
var instructor = "Axle Barr";
class Course {
constructor(courseName, instructor) {
this.courseName = courseName;
this.instructor = instructor;
};
about() {
return this.courseName " is being taught by " this.instructor;
}
};
function choiceMade(choice, courseChosen) {
document.getElementById("courseName").value = choice;
currentCourse = new Course(courseChosen, instructor); //object
document.getElementById("courseInfo").innerHTML = currentCourse.about();

uj5u.com熱心網友回復:
一切都是正確的期待函式宣告。您可以替換功能 choiceMade 如下 -
function choiceMade(choice) {
document.getElementById("courseName").value = choice;
currentCourse = new Course(choice, instructor); //object
document.getElementById("courseInfo").innerHTML = currentCourse.about();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528282.html
上一篇:帶有宣告變數的SQL函式
