【问题标题】:Why is my HTML page reloading every time a new entry is submitted into a form?为什么每次将新条目提交到表单时我的 HTML 页面都会重新加载?
【发布时间】:2019-10-22 12:23:11
【问题描述】:

我的 HTML 页面有问题。我的程序旨在使用表格收集姓名,然后将它们输出到表格下方的表格中。当页面没有重新加载时,它能够做到这一点。

当我第一次输入某个名称时,页面会重新加载。页面重新加载后,我输入了相同的名称,以后按回车键就不会重新加载。我不知道这是为什么,也不知道如何解决。

这里是链接的JS文件

// Gloabal Variables
var enteredName,
  countOutput,
  count,
  table,
  form,
  allNames = [];

function project62Part2() {
  // Your code goes in here.
  function getElements() {
    form = document.getElementById("nameForm");
    countOutput = document.getElementById("numNames");
    table = document.getElementById("table");
  }

  function addName() {
    enteredName = form.name.value;
    allNames.push(enteredName);
  }

  function countNames() {
    // Reset count
    count = 0;

    // Loop through and count names
    for (i = 0; i < allNames.length; i++) {
      count++;
    }
  }

  function output() {
    // Reset table
    table.innerHTML = "<tr><th>Names</th></tr>";
    // Display count
    countOutput.innerHTML = "Total names entered: " + count;

    // Loop through and add to table display
    for (i = 0; i < allNames.length; i++) {
      table.innerHTML += "<tr><td>" + allNames[i] + "</td></tr>";
    }
  }

  // Call code
  getElements();
  addName();
  countNames();
  output();

  // Prevent page from reloading
  return false;
}
<form id="nameForm" action="#">
  <label class="formLabel" for="name">Name: </label>
  <input id="name" name="name" />

  <input type="submit" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2()" />
</form>

<div id="numNames">Total names entered: </div>

<table id="table"></table>

我对编码的理解充其量只是初步的,所以虽然我很感激任何答案,但我希望它保持简单!

【问题讨论】:

  • 只使用 JavaScript 自己:formElement.onsubmit = project62Part2;

标签: javascript html forms reload


【解决方案1】:

&lt;input type='submit'&gt; 导致页面刷新。将其替换为&lt;input type='button'&gt;

阅读更多here

        // Gloabal Variables
    var enteredName,
    	countOutput,
    	count,
        table,
    	form,
    	allNames = [];
    
    function project62Part2() {
        // Your code goes in here.
        function getElements() {
    		form = document.getElementById("nameForm");
    		countOutput = document.getElementById("numNames");
    		table = document.getElementById("table");
    	}
        
    	function addName() {
    		enteredName = form.name.value;
    		allNames.push(enteredName);
    	}
    	
    	function countNames() {
    		// Reset count
    		count = 0;
    		
    		// Loop through and count names
    		for (i = 0; i < allNames.length; i++) {
    			count++;
    		}
    	}
    	
    	function output() {
    		// Reset table
    		table.innerHTML = "<tr><th>Names</th></tr>";
    		// Display count
    		countOutput.innerHTML = "Total names entered: " + count;
    		
    		// Loop through and add to table display
    		for (i = 0; i < allNames.length; i++) {
    			table.innerHTML += "<tr><td>" + allNames[i] + "</td></tr>";
    		}
    	}
    	
    	// Call code
    	getElements();
    	addName();
    	countNames();
    	output();
    
    	// Prevent page from reloading
    	return false;
    }
    <form id="nameForm" action="6.2projectpart2.html#">
        <label class="formLabel" for="name">Name: </label>
        <input id="name" name="name" />
        
        <input type="button" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2()" />
    </form>
    
    <div id="numNames">Total names entered: </div>
    
    <table id="table"></table>

【讨论】:

    【解决方案2】:

    您可以从&lt;input type="submit" name="runExample" /&gt;更改为

    <input type="button" name="runExample" />
    

    删除输入标签上的onclick="project62Part2()"并移动到表单标签onsubmit="return project62Part2()"

    <form id="nameForm" onsubmit="return project62Part2()">
    

    【讨论】:

      【解决方案3】:

      有很多方法可以实现这一点,我将解释两种方法:

      1。添加Event.preventDefault()方法

      每当您单击submit 类型的&lt;input&gt; 元素时,用户代理都会尝试将表单提交到表单中的URL 设置。

      现在,preventDefault() 方法告诉用户代理,如果事件没有得到显式处理,则不应像通常那样采取其默认操作。这意味着Form 接口不会重新加载页面。

      这个怎么运作?
      • 好吧,只需将事件变量添加到您的 submit 调用中,如下所示:
      <input type="submit" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2(event)" />
      
      • 然后将event参数添加到project62Part2()方法中。
      function project62Part2(event) {
         event.preventDefault();
      
         ...
      
      }
      

      // Gloabal Variables
      var enteredName,
        countOutput,
        count,
        table,
        form,
        allNames = [];
      
      function project62Part2(event) {
         event.preventDefault();
        // Your code goes in here.
        function getElements() {
          form = document.getElementById("nameForm");
          countOutput = document.getElementById("numNames");
          table = document.getElementById("table");
        }
      
        function addName() {
          enteredName = form.name.value;
          allNames.push(enteredName);
        }
      
        function countNames() {
          // Reset count
          count = 0;
      
          // Loop through and count names
          for (i = 0; i < allNames.length; i++) {
            count++;
          }
        }
      
        function output() {
          // Reset table
          table.innerHTML = "<tr><th>Names</th></tr>";
          // Display count
          countOutput.innerHTML = "Total names entered: " + count;
      
          // Loop through and add to table display
          for (i = 0; i < allNames.length; i++) {
            table.innerHTML += "<tr><td>" + allNames[i] + "</td></tr>";
          }
        }
      
        // Call code
        getElements();
        addName();
        countNames();
        output();
      
        // Prevent page from reloading
        return false;
      }
      <form id="nameForm" action="#">
        <label class="formLabel" for="name">Name: </label>
        <input id="name" name="name" />
      
        <input type="submit" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2(event)" />
      </form>
      
      <div id="numNames">Total names entered: </div>
      
      <table id="table"></table>

      2。将input 替换为button

      这是基于前面的解释。如果submit 类型的&lt;input&gt; 元素触发了提交调用,那么如果将其替换为button 类型,则不会触发提交调用。如果您正在处理服务器调用,我建议您这样做以维护submit

      这个怎么运作?
      • 好吧,只需将类型从 submit 替换为 button,如下所示:
      <input type="button" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2()" />
      

      // Gloabal Variables
      var enteredName,
        countOutput,
        count,
        table,
        form,
        allNames = [];
      
      function project62Part2() {
        event.preventDefault();
        // Your code goes in here.
        function getElements() {
          form = document.getElementById("nameForm");
          countOutput = document.getElementById("numNames");
          table = document.getElementById("table");
        }
      
        function addName() {
          enteredName = form.name.value;
          allNames.push(enteredName);
        }
      
        function countNames() {
          // Reset count
          count = 0;
      
          // Loop through and count names
          for (i = 0; i < allNames.length; i++) {
            count++;
          }
        }
      
        function output() {
          // Reset table
          table.innerHTML = "<tr><th>Names</th></tr>";
          // Display count
          countOutput.innerHTML = "Total names entered: " + count;
      
          // Loop through and add to table display
          for (i = 0; i < allNames.length; i++) {
            table.innerHTML += "<tr><td>" + allNames[i] + "</td></tr>";
          }
        }
      
        // Call code
        getElements();
        addName();
        countNames();
        output();
      
        // Prevent page from reloading
        return false;
      }
      <form id="nameForm" action="#">
        <label class="formLabel" for="name">Name: </label>
        <input id="name" name="name" />
      
        <input type="button" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2()" />
      </form>
      
      <div id="numNames">Total names entered: </div>
      
      <table id="table"></table>

      【讨论】:

        【解决方案4】:

        尝试在 project62Part2 上添加一个事件参数,然后在里面做一个 event.preventDefault()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-29
          • 2012-03-02
          • 1970-01-01
          • 2013-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多