【发布时间】:2016-12-12 06:24:25
【问题描述】:
我要解决的问题是如何将选项数组作为一组或单选按钮打印到我的 HTML 中;或者将数组 a_options[ ] 中的每个项目单独传递给 HTML 中已经存在的单选按钮。
我知道answer1.display(a_option[0]); 不会产生任何结果,因为a_option[ ] 不是显示中定义的变量。
关于如何做到这一点的任何想法或可能吗?
感谢您的提示和想法!
// My constructor function & class 'Questions'
var Questions = function(q_text)
{
this.q_text = q_text;
};
// My function for displaying the key-value pair 'q_text' in 'Questions' class.
Questions.prototype.displayQ = function()
{
return this.q_text;
};
// My newObj created from class 'Questions'.
var question1 = new Questions("Who is the winningest coach at KU?");
// question1 has taken on the protoype of 'q_text'
// Displays the key-value pair 'q_text' where 'q_text' has taken on the value
// passed through the constructor function 'Questions'.
question1.displayQ();
//#######################
// My constructor function & class 'Answers'
var Answers = function(a_options, a_facts, a_img, answer)
{
this.a_options = a_options;
this.a_facts = a_facts;
this.a_img = a_img;
this.answer = answer;
};
// My function for displaying the key-value pair 'a_options' in 'Answers' class.
Answers.prototype.displayA = function()
{
return this.a_options;
// return this.a_facts;
// return this.a_img;
// return this.answer;
};
// My newObj created from class 'Answers'
var answer1 = new Answers(['Bill Self', 'Phog Allen', 'Larry Brown', 'James Naismith', 'Roy Williams'], "Bill Self is 372-82 (.822) during his 14 years at the helm of Kansas.", "img/self.jpg", "Bill Self");
// question1 has taken on the protoype of 'a_options'
// Displays the key-value pair 'a_options' where 'a_options' has taken on the value
// passed through the constructor function 'Answers'.
answer1.displayA();
【问题讨论】:
-
什么是
answer1.display()?您在Answer原型中定义的唯一函数是displayA()。 -
您可以使用
answer1.a_options[0]获取a_options数组的元素之一。 -
answer1.displayA()返回此数组。您可以简单地遍历数组,将它们添加到您的 HTML 中。 -
@Barmar 这是我的错误,最初只有
display( ),但有一个问答类,最好让displayQ( )和displayA( )错过更正。感谢您的建议和帮助,我真的很感激! -
那些函数名称错误。他们不显示任何东西,他们只是返回东西。如果您希望它们显示,则需要由调用者完成。
标签: javascript arrays oop constructor