简体   繁体   English

如何将JS函数转换为HTML表单?

[英]How do I get a JS function resut to a HTML form?

I'm trying to build an app in js that would get a random question from an array that would combine with a random object from another array. 我正在尝试在js中构建一个应用程序,该应用程序将从一个数组中获取一个随机问题,该数组将与另一个数组中的一个随机对象结合在一起。 So my app looks like this: 所以我的应用看起来像这样:

Array.prototype.random = function (length) {
    return this[Math.floor(Math.random()*length)];
};

var country = [
    {name:"Romania", capital: "Bucuresti"},
    {name: "Bulgariei", capital: "Sofia"}
];

chosen_country = country.random(country.length);

var questions = [
    "Which is the capital of ",
    " is the capital of which country?"
];

var chosen_question = questions.random(questions.length);

function q() {
    chosen_country;
    chosen_question;
    if(chosen_question == questions[0]) {
        return chosen_question + chosen_country.name + "?";
    } else if(chosen_question == questions[1]) {
        return chosen_country.capital + chosen_question;
    }
}

q();

So my app will generate a random question from the questions array and combine it with a random object from the country array: 因此,我的应用将根据问题数组生成一个随机问题,并将其与来自国家/地区数组的随机对象组合:

  1. "Which is the capital of 'country x' ?" “'x国'的首都是哪个?”
  2. "'Capital x' is the capital of which country?" “'x首都'是哪个国家的首都?”

I would like to know how get the random question generated with a button form in HTML. 我想知道如何获得用HTML中的按钮形式生成的随机问题。 Afterwords I would like to answer the question within an input form and send the answer to be use in another function to check if the answer is corector not. 后记我想在输入表格中回答问题,然后将答案发送给另一个函数,以检查答案是否正确。 Anyone having any idea? 有任何想法吗?

I have tried to use document.getElementById("que").innerHTML = q(); 我试图使用document.getElementById("que").innerHTML = q(); but I realy don't know how to use it properly. 但我真的不知道如何正确使用它。 Is there any other solution or I could use some explanations how to use .innerHTML . 还有其他解决方案吗,或者我可以使用一些解释说明如何使用.innerHTML

Here is a different way of setting up the whole thing. 这是设置整个事情的另一种方法。 You might find some of the ideas useful. 您可能会发现一些有用的想法。

  • no library (pure DOM interaction) 没有库(纯DOM交互)
  • more question possibilities than just country and capital 不仅仅是国家和首都,更多的问题可能性
  • a simple string formatter for building the question text 用于构建问题文本的简单字符串格式化程序

 Array.prototype.random = function () { return this[Math.floor(Math.random() * this.length)]; }; String.prototype.trim = function () { return this.replace(/^\\s+|\\s+$/g, ""); }; // ----------------------------------------------------------------------- function randomQuestion(bundle) { var fact = bundle.facts.random(), question = bundle.questions.random(); return { text: question.text.replace(/\\{([^}]+)\\}/, function($0, $1) { return fact[$1]; }), answer: fact[question.compare] }; } // ----------------------------------------------------------------------- var countryBundle = { facts: [ {country: "Romania", capital: "Bucuresti", continent: "Europe"}, {country: "Bulgaria", capital: "Sofia", continent: "Europe"}, {country: "Germany", capital: "Berlin", continent: "Europe"}, {country: "France", capital: "Paris", continent: "Europe"}, {country: "India", capital: "Delhi", continent: "Asia"} ], questions: [ {text: "Which is the capital of {country}?", compare: 'capital'}, {text: "{capital} is the capital of which country?", compare: 'country'}, {text: "On what continent lies {country}?", compare: 'continent'} ] }; // ----------------------------------------------------------------------- function setupForm() { var questionLabel = document.getElementById("question"), answerInput = document.getElementById("answer"), currentQuestion, showNextQuestion = function () { currentQuestion = randomQuestion(countryBundle); questionLabel.textContent = currentQuestion.text; answerInput.value = ""; }; showNextQuestion(); document.getElementById("nextQuestion").onclick = showNextQuestion; document.getElementById("enter").onclick = function () { var correctAnswer = currentQuestion.answer.trim().toLowerCase(), givenAnswer = answerInput.value.trim().toLowerCase(); if (correctAnswer === givenAnswer) { alert("Yes :)"); showNextQuestion(); } else { alert("No :("); } }; } setupForm(); 
 <button id="nextQuestion">Next question</button><br> <label for="answer" id="question">&nbsp;</label><br> <input id="answer"> <button id="enter">OK</button> 

I've changed your code a bit. 我对您的代码做了一些更改。 I'm using querySelector , which is more powerful than getElementById , but you'll need to use a pound sign (#) when referring to elements with Ids. 我正在使用querySelector ,它比getElementById更强大,但是在引用具有Ids的元素时需要使用井号(#)。

I've also moved your click handler from the HTML into the JavaScript, which is best practice. 我还将您的点击处理程序从HTML移到了JavaScript,这是最佳做法。

Each time you click the button, que will show a random question: 每次单击按钮, que都会显示一个随机问题:

 Array.prototype.random = function (length) { return this[Math.floor(Math.random()*length)]; }; var country = [ {name:"romaniei", capital: "bucuresti"}, {name: "bulgariei", capital: "sofia"} ], questions = [ "What is the capital of ", " is the capital of what country?" ] document.querySelector('input').onclick= function() { var q, chosen_country = country.random(country.length), chosen_question = questions.random(questions.length); if(chosen_question == questions[0]){ q= chosen_question + chosen_country.name + "?"; } else if(chosen_question == questions[1]){ q= chosen_country.capital + chosen_question; } document.querySelector('#que').innerHTML= q; } 
 <form name="myform"> <input type="button" value="Generate question"> <div id="que">Intrebare:</div> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM