简体   繁体   English

在这种情况下如何显示字符串?

[英]How do I display a string in this case?

I want to see if I can display a string, however it seems that it doesn't function as intended.我想看看我是否可以显示一个字符串,但是它似乎没有按预期工作。 Am I declaring the string wrong?我声明字符串错误吗? Or am I supposed to use something else?还是我应该使用其他东西?

Here is the HTML of my code:这是我的代码的 HTML:

  <div class="modal" id="fruit">
            <!-- Modal content -->
            <div class="modal_content">
                <span class="close">&times;</span>
                <p>what fruit starts with an A and ends with a E, and grows on trees?</p>
                <form name="fruit_form">
                    <label>Type your answer here:</label>
                    <input type="text" name="fruit_answer" class="user_input">
                    <input type="submit" onclick="check_ans1()" id="submit_button1">
                    <p id="show_user_input"></p>

                </form>
            </div>
        </div>

        <!-- the popup for getting the answer incorrect-->
        <div class="check_ans_modal" id="incorrect_popup">
            <div class="wrong_ans">
                <h1>WRONG!!!!</h1>
                <p>the answer was:</p> 
                <div class="correct_ans_text"></div>
            </div>
        </div>

Here is the javascript of my code:这是我的代码的javascript:

//stop webpage from reloading when press submit button
document.getElementById("submit_button1").addEventListener("click",
  function (event) {
    event.preventDefault()
  });

// POPUP when user gets question incorrect
function display_incorrect(a) {
  document.getElementById("incorrect_popup").style.display = "block";
  document.querySelector(".correct_ans_text").innerHTML = a;
}

let fruit_ans= "apple";

  function check_ans1() {
  let x = document.forms["fruit_form"]["fruit_answer"].value;
  if (x == "") {
    alert("Answer must be filled out!!!");
  }
  else if (x == fruit_ans) {
    display_correct();
  }
  else {
    alert("you entered: " + x); 
    display_incorrect(fruit_ans);
  }
}

Unfortunately, Im not sure why the submit button does not react at all when I press it.不幸的是,我不确定为什么当我按下提交按钮时根本没有反应。 But when I leave the textbox blank and submit it, the alert works.但是当我将文本框留空并提交时,警报会起作用。 So turns out its just the else if and the else statements that are not working.事实证明,它只是else ifelse语句不起作用。

<input type="text" name="fruit_answer" class="user_input" id="some-id">

document.getElementById("submit_button1").addEventListener("click",
  function (event) {
    // If you put this here... submit no longer works unless you take over and do something...
    event.preventDefault()
so...
let inputVal = document.getElementById("some-id").value
check_ans1(inputVal)
  });
// And down here...
function check_ans1(theSameInputValue) {
 // do your magic here...
}

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

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