简体   繁体   English

为什么即使表单调用的函数末尾存在“ return false”,表单上的“提交”按钮仍会刷新页面?

[英]Why does the 'submit' button on form keep refreshing the page even with presence of 'return false' at the end of the functioned called by the form?

In this assignment I'm supposed to "Write an application that searches for words in an array and creates a new array of found words and an array of search words that are not found. The application will display the new arrays of found and not-found words every time a new word is entered." 在本作业中,我应该“编写一个应用程序,该应用程序在数组中搜索单词,并创建一个新的找到的单词数组和一个未找到的搜索单词数组。该应用程序将显示found的新数组,而不显示-每次输入新单词时都找到了单词。” I'm not sure what the problem is in my code, but every time I press 'submit on the web page it refreshes. 我不确定代码中的问题是什么,但是每次我在网页上按“提交”都会刷新。 Nothing is outputted correctly. 没有正确输出任何内容。

Unfortunately, this is an assignment I've left alone for a long time, but now that it's getting to be the end of the school year, I need to get it done. 不幸的是,这是我一个人已经待了很长时间的作业,但是现在已经快要结束学年了,我需要完成它。 I do not have as good an understanding of JavaScript as I did when I was initially working on it. 我对JavaScript的了解不如我最初从事JavaScript时的了解。 I fiddled with it for a bit, but couldn't figure out what was wrong. 我摆弄了一下,但不知道出了什么问题。 I will provide the relevant code from both the html and js file. 我将提供来自html和js文件的相关代码。

HTML File HTML文件

<form id="searchFormId" action="6.2assignment2.html#" onsubmit="return assignment62_2Part1();">

  <label class="formLabel" for="entry">Entry: </label>
  <input id="entry" name="entry">

  <input type="submit" name="runExample" value="Enter" class="formatSubmit">

    </form>

    <!-- Ordered list of found words -->
    <h>Found Words</h>
    <ol id="foundWordsList"></ol>

    <!-- Ordered list of unfound words -->
    <h>Unfound Words</h>
    <ol id="unfoundWordsList"></ol>

JS File JS文件

var rightWords = ["JavaScript", "was", "developed", "by", "Brendan", "Eich",
                  "at", "Netscape", "as", "the", "in-page", "scripting",
                  "language", "for", "Navigator", "2.", "It", "is", "a",
                  "remarkably", "expressive", "dynamic", "programming",
                  "language.", "Because", "of", "its", "linkage", "to", "web",
                  "browsers", "it", "instantly", "became", "massively",
                  "popular.", "It", "never", "got", "a", "trial", "period",
                  "in", "which", "it", "could", "be", "corrected", "and",
                  "polished", "based", "on", "actual", "use", "The",
                  "language", "is", "powerful", "and", "flawed"];

// Get elements
var form = document.getElementById("searchFormId");
var foundList = document.getElementById("foundWordsList");
var unfoundList = document.getElementById("unfoundWordsList");

// Variable to hold user entry
var userEntry;

// Found/unfound word arrays
var foundWords = [];
var unfoundWords = [];

    function assignment62_2Part1() {
        // PART 1: YOUR CODE STARTS AFTER THIS LINE
        // What happens after Enter is pressed

        // Check if word was found
        var valueFound = false;

        // Reset output
        foundList.innerHTML = "";
        unfoundList.innerHTML = "";

        // Get user response 
        userEntry = form.entry.value;

        // Search and compare
        var i;
        for (i = 0; i < rightWords; i++) {
            if (userEntry === rightWords[i]) {
                valueFound = true;
                break;
            }
        }

        if (valueFound) {
            foundWords.push(userEntry);
        } else {
                unfoundWords.push(userEntry);
        }   

        // Output
        for (i = 0; i < foundWords; i++) {
            foundList.innerHTML += "<li>" + foundWords[i] + "</li>";
        }

        for (i = 0; i < unfoundWords; i++) {
            unfoundList.innerHTML += "<li>" + unfoundWords[i] + "</li>";
        } 

        return false;
    }

I know I brought this onto myself by procrastinating, but I'm in a real bind! 我知道我是通过拖延把这件事带到自己身上的,但是我确实处于束缚之中! Any help is appreciated! 任何帮助表示赞赏! Also, apologies if I've broken site etiquette. 另外,如果我违反了网站礼节,则表示歉意。 This is my first time looking at it much, not to mention making a post. 这是我第一次关注它,更不用说发布帖子了。

Also worth noting, this is for a semester-long class in high school, nothing fancy. 同样值得注意的是,这是高中一学期的课程,没什么花哨的。

Edit: To clarify, I have been led to believe in the course of this curriculum that simple adding 'return false' at the end of the function that is called upon by the form is enough to prevent the page from being reloaded. 编辑:为澄清起见,在本课程的过程中,我被引导相信在表单调用的函数末尾简单添加“ return false”足以防止重新加载页面。 I have completed other assignments where it's worked just fine, but doesn't in this one. 我已经完成了其他工作,但效果很好,但在本次工作中却没有。 I'm asking for help with finding what in my code is preventing it from doing what the assignment asks it to, as written in the first paragraph of the post. 我正在寻求帮助,以查找我的代码中有哪些内容阻止该代码执行作业要求的内容,如该帖子的第一段所述。

您可以尝试在HTML中使用<button type="button> </button> ,它不应刷新页面,并且您可以在按钮上使用js click事件来继续应用程序的下一步操作。如果表单未填写,您可以使用js来使按钮具有不允许的光标样式。

Here you go. 干得好。 Hope it helps you. 希望对您有帮助。 :) :)

 var rightWords = ["JavaScript", "was", "developed", "by", "Brendan", "Eich", "at", "Netscape", "as", "the", "in-page", "scripting", "language", "for", "Navigator", "2.", "It", "is", "a", "remarkably", "expressive", "dynamic", "programming", "language.", "Because", "of", "its", "linkage", "to", "web", "browsers", "it", "instantly", "became", "massively", "popular.", "It", "never", "got", "a", "trial", "period", "in", "which", "it", "could", "be", "corrected", "and", "polished", "based", "on", "actual", "use", "The", "language", "is", "powerful", "and", "flawed" ]; // Get elements var form = document.getElementById("searchFormId"); var foundList = document.getElementById("foundWordsList"); var unfoundList = document.getElementById("unfoundWordsList"); // Variable to hold user entry var userEntry; // Found/unfound word arrays var foundWords = []; var unfoundWords = []; function assignment62_2Part1() { // Check if word was found var valueFound = false; // Reset output foundList.innerHTML = ""; unfoundList.innerHTML = ""; // Get user response userEntry = entry.value; // Search and compare var i; for (i = 0; i < rightWords.length; i++) { if (userEntry === rightWords[i]) { valueFound = true; break; } } if (valueFound) { foundWords.push(userEntry); } else { unfoundWords.push(userEntry); } // Output for (i = 0; i < foundWords.length; i++) { foundList.innerHTML += "<li>" + foundWords[i] + "</li>"; } for (i = 0; i < unfoundWords.length; i++) { unfoundList.innerHTML += "<li>" + unfoundWords[i] + "</li>"; } return false; } 
 <label class="formLabel" for="entry">Entry: </label> <input id="entry" name="entry"> <input type="button" name="runExample" value="Enter" class="formatSubmit" onclick="assignment62_2Part1();" /> <!-- Ordered list of found words --> <h4>Found Words</h4> <ol id="foundWordsList"></ol> <!-- Ordered list of unfound words --> <h4>Unfound Words</h4> <ol id="unfoundWordsList"></ol> 

Here is a more advanced and easier version to help you improve your code in the future :) 这是一个更高级,更轻松的版本,可帮助您将来改进代码:)

 var rightWords = ["JavaScript", "was", "developed", "by", "Brendan", "Eich", "at", "Netscape", "as", "the", "in-page", "scripting", "language", "for", "Navigator", "2.", "It", "is", "a", "remarkably", "expressive", "dynamic", "programming", "language.", "Because", "of", "its", "linkage", "to", "web", "browsers", "it", "instantly", "became", "massively", "popular.", "It", "never", "got", "a", "trial", "period", "in", "which", "it", "could", "be", "corrected", "and", "polished", "based", "on", "actual", "use", "The", "language", "is", "powerful", "and", "flawed" ]; var foundWords = []; var unfoundWords = []; btn.onclick = ()=>{ (rightWords.indexOf(entry.value) > 0 ? foundWords : unfoundWords) .push("<li>" + entry.value + "</li>"); foundWordsList.innerHTML = foundWords.join(''); unfoundWordsList.innerHTML = unfoundWords.join(''); } 
 <label class="formLabel" for="entry">Entry: </label> <input id="entry" name="entry"> <input type="button" id='btn' name="runExample" value="Enter" class="formatSubmit" /> <!-- Ordered list of found words --> <h4>Found Words</h4> <ol id="foundWordsList"></ol> <!-- Ordered list of unfound words --> <h4>Unfound Words</h4> <ol id="unfoundWordsList"></ol> 

It's typically the opportunity to use a set 通常是使用一套的机会
=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set => https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set

but you also may use Array​.prototype​.includes() 但您也可以使用Array.prototype.includes()
=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes => https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

to diseable page reload you have to use : Event​.prevent​Default() => https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault 要禁用页面重新加载,您必须使用: Event..preventDefault() => https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault

but this one works on the form, not on the button, has I show it in my snippet : 但是我可以在代码段中显示它,但它适用于表单而不是按钮:

 const form = document.getElementById('searchFormId') , foundList = document.getElementById('foundWordsList') , unfoundList = document.getElementById('unfoundWordsList') , rightWords = new Set( [ 'JavaScript', 'was', 'developed', 'by', 'Brendan', 'Eich' , 'at', 'Netscape', 'as', 'the', 'in-page', 'scripting' , 'language', 'for', 'Navigator', '2.', 'It', 'is', 'a' , 'remarkably', 'expressive', 'dynamic', 'programming' , 'language.', 'Because', 'of', 'its', 'linkage', 'to', 'web' , 'browsers', 'it', 'instantly', 'became', 'massively' , 'popular.', 'It', 'never', 'got', 'a', 'trial', 'period' , 'in', 'which', 'it', 'could', 'be', 'corrected', 'and' , 'polished', 'based', 'on', 'actual', 'use', 'The' , 'language', 'is', 'powerful', 'and', 'flawed' ]) ; form.onsubmit = function(evt) { evt.preventDefault(); // disable form sending action let userEntry = form.entry.value , valueFound = rightWords.has( userEntry ) , LI_elm = document.createElement('li') ; form.entry.value = ''; LI_elm.textContent = userEntry; if (valueFound) { foundList.appendChild( LI_elm ) } else { unfoundList.appendChild( LI_elm ) } } 
 <form id="searchFormId" > <label class="formLabel" for="entry">Entry: </label> <input id="entry" name="entry"> <button type="submit" class="formatSubmit"> Enter </button> </form> <!-- Ordered list of found words --> <h2>Found Words</h2> <ol id="foundWordsList"></ol> <!-- Ordered list of unfound words --> <h2>Unfound Words</h2> <ol id="unfoundWordsList"></ol> 

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

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