使用Javascript HTML公式

使用Javascript HTML公式,javascript,html,forms,button,onclick,Javascript,Html,Forms,Button,Onclick,首先,我可能有不同的问题,对于想知道你的名字和姓氏的人来说,这是很正常的(是德语,但我希望是这样) 姓名: 纳希纳姆: 然后我想使用一个函数,它从表单中获取姓名,然后像留言簿一样显示所有填写该表单的人的姓名。到目前为止,我只是让它调用一个警报,它说“你好”,但我甚至不知道如何使用表单输入作为变量 我希望您能帮助我,请只使用JS和HTML。您可以捕获字段并将其添加到数组中,然后将该数组打印到页面上。 这是一个包含我的代码的文件。现有的add按钮附加到表单上方的ul元素,并将名称保存到

首先,我可能有不同的问题,对于想知道你的名字和姓氏的人来说,这是很正常的(是德语,但我希望是这样)



姓名:

纳希纳姆:

然后我想使用一个函数,它从表单中获取姓名,然后像留言簿一样显示所有填写该表单的人的姓名。到目前为止,我只是让它调用一个警报,它说“你好”,但我甚至不知道如何使用表单输入作为变量


我希望您能帮助我,请只使用JS和HTML。

您可以捕获字段并将其添加到数组中,然后将该数组打印到页面上。 这是一个包含我的代码的文件。现有的add按钮附加到表单上方的ul元素,并将名称保存到全局数组中。我还添加了“清除”和“加载”按钮,以便您可以看到如何从数组填充列表

<ul id="guestList">

</ul>
<form name="formular" action="" onsubmit="">
    <p>
        <label for="vorname">Vorname:</label>
        <input value="" type="text" name="vorname" id="vorname" size="25"/>
    </p>
<p>
    <label for="nachname">Nachname:</label>
    <input type="text" name="nachname" id="nachname" size="25"/>
</p>
<p>
    <input type="button" value="Absenden" onclick="storeVals();" />
    <input type="button" value="Clear List" onclick="clearList();" />
    <input type="button" value="Load List" onclick="loadList();" />
</p>
</form>
姓名:

纳希纳姆:

和javascript:

var names = []; //global scope

function storeVals() //declare function to store the input
{
    //document.getElementById finds the element with the 
    //corresponding ID. In this case I'm getting the objects 
    //behind each of the input boxes, and retrieving their value properties 
    //(the text entered in them). I'm also concatenating them together with
    //the plus sign (+) operator, and adding a space between them with the ' '
    //I'm storing the result of all that in a variable called name.
    var name = document.getElementById('vorname').value + ' ' +     document.getElementById('nachname').value;

    //I'm accessing the names array I declared above. The elements in the
    //array are accessed using an index which counts from 0.
    //So to get the first element, I would do names[0].
    //In this case I'm using names.length instead of a number, as it will return
    //the number of elements currently in the array. When there are zero, it will return 0.
    //When there is one, it will return 1, which happens to be the index for the second element.
    //The means I'm always assigning the name to the element after the last existing one.
    names[names.length] = name;
    alert(names); //just to help see what's going on. Remove this when you don't need it anymore.

    //Uses document.getElementById again, this time to get the object behind my ul list.
    //And calls my appendToList function below, passing it my name variable and a 
    //reference to the list object.
    appendToList(name, document.getElementById('guestList')); 
}

function appendToList(name,ulist){
    //this constructs a <li> object and stores it in a variable.
    var li = document.createElement("li");
    //this adds the text of the name inside the <li>
    //so if name is equal to "john smith", I've built:
    //<li>john smith</li>
    li.appendChild(document.createTextNode(name));
    //adds the li as a child of my existing list object.
    //so:
    //   <ul id="guestList"><li>john smith</li></ul>
    ulist.appendChild(li);
}

function clearList(){
    //gets my ul list object with the id "guestList"
    //sets its innerHTML (all of its contents) to an empty string
    //so regardless of what's inside, it becomes <ul id="guestList"></ul>
    document.getElementById('guestList').innerHTML = '';
}

function loadList(){
    //got my ul list object by id again.
    var list = document.getElementById('guestList');
    //for loop - essentially this means start the variable i at 0
    //keep looping while i < names.length, and increment i by 1
    //each time we iterate through the loop.
    for(i=0;i < names.length; i++){
        //call the appendToList function above, and pass it names[i] and my list object
        //so essentially, loop over every name in the array and call that function with
        //it to add the name to the list.
        appendToList(names[i],list);
    }
}
var name=[]//全球范围
函数storeVals()//声明用于存储输入的函数
{
//document.getElementById查找具有
//对应的ID。在这种情况下,我得到的是对象
//在每个输入框后面,并检索其值属性
//(输入的文本)。我还将它们与
//加号(+)运算符,并在它们之间添加带有“”的空格
//我将所有这些结果存储在一个名为name的变量中。
var name=document.getElementById('vorname').value+''+document.getElementById('nachname').value;
//我正在访问上面声明的名称数组
//使用从0开始计数的索引访问数组。
//因此,要获得第一个元素,我需要使用名称[0]。
//在本例中,我使用names.length而不是数字,因为它将返回
//数组中当前的元素数。如果为零,则返回0。
//当有一个元素时,它将返回1,它恰好是第二个元素的索引。
//这意味着我总是在最后一个元素之后为元素指定名称。
名称[名称.长度]=名称;
警报(名称);//只是为了帮助查看发生了什么。当您不再需要它时,请将其删除。
//再次使用document.getElementById,这一次获取ul列表后面的对象。
//并调用下面的appendToList函数,将我的name变量和
//对列表对象的引用。
appendToList(名称,document.getElementById('guestList');
}
函数附录列表(名称,ulist){
//这将构造一个
  • 对象并将其存储在变量中。 var li=document.createElement(“li”); //这会将名称的文本添加到
  • //所以,如果名称等于“约翰·史密斯”,我就建立: //约翰·史密斯
  • li.appendChild(document.createTextNode(name)); //将li添加为现有列表对象的子对象。 //因此: //约翰·史密斯 ulist.appendChild(li); } 函数clearList(){ //获取id为“guestList”的我的ul列表对象 //将其innerHTML(所有内容)设置为空字符串 //因此,不管里面是什么,它都会变成
      document.getElementById('guestList')。innerHTML=''; } 函数loadList(){ //再次按id获取我的ul列表对象。 var list=document.getElementById('guestList'); //for loop-本质上这意味着变量i从0开始 //在i
      这里有一些我做过的一些事情的具体参考资料


      但是您需要注意,如果您只是使用客户端javascript,那么这些数据将不会在页面刷新后继续存在,并且如果从其他浏览器访问页面,这些数据将不可用。

      您可以捕获字段并将其添加到数组中,然后将该数组打印到页面上。 这是一个包含我的代码的文件。现有的add按钮附加到表单上方的ul元素,并将名称保存到全局数组中。我还添加了“清除”和“加载”按钮,以便您可以看到如何从数组填充列表

      <ul id="guestList">
      
      </ul>
      <form name="formular" action="" onsubmit="">
          <p>
              <label for="vorname">Vorname:</label>
              <input value="" type="text" name="vorname" id="vorname" size="25"/>
          </p>
      <p>
          <label for="nachname">Nachname:</label>
          <input type="text" name="nachname" id="nachname" size="25"/>
      </p>
      <p>
          <input type="button" value="Absenden" onclick="storeVals();" />
          <input type="button" value="Clear List" onclick="clearList();" />
          <input type="button" value="Load List" onclick="loadList();" />
      </p>
      </form>
      
      姓名:

      纳希纳姆:

      和javascript:

      var names = []; //global scope
      
      function storeVals() //declare function to store the input
      {
          //document.getElementById finds the element with the 
          //corresponding ID. In this case I'm getting the objects 
          //behind each of the input boxes, and retrieving their value properties 
          //(the text entered in them). I'm also concatenating them together with
          //the plus sign (+) operator, and adding a space between them with the ' '
          //I'm storing the result of all that in a variable called name.
          var name = document.getElementById('vorname').value + ' ' +     document.getElementById('nachname').value;
      
          //I'm accessing the names array I declared above. The elements in the
          //array are accessed using an index which counts from 0.
          //So to get the first element, I would do names[0].
          //In this case I'm using names.length instead of a number, as it will return
          //the number of elements currently in the array. When there are zero, it will return 0.
          //When there is one, it will return 1, which happens to be the index for the second element.
          //The means I'm always assigning the name to the element after the last existing one.
          names[names.length] = name;
          alert(names); //just to help see what's going on. Remove this when you don't need it anymore.
      
          //Uses document.getElementById again, this time to get the object behind my ul list.
          //And calls my appendToList function below, passing it my name variable and a 
          //reference to the list object.
          appendToList(name, document.getElementById('guestList')); 
      }
      
      function appendToList(name,ulist){
          //this constructs a <li> object and stores it in a variable.
          var li = document.createElement("li");
          //this adds the text of the name inside the <li>
          //so if name is equal to "john smith", I've built:
          //<li>john smith</li>
          li.appendChild(document.createTextNode(name));
          //adds the li as a child of my existing list object.
          //so:
          //   <ul id="guestList"><li>john smith</li></ul>
          ulist.appendChild(li);
      }
      
      function clearList(){
          //gets my ul list object with the id "guestList"
          //sets its innerHTML (all of its contents) to an empty string
          //so regardless of what's inside, it becomes <ul id="guestList"></ul>
          document.getElementById('guestList').innerHTML = '';
      }
      
      function loadList(){
          //got my ul list object by id again.
          var list = document.getElementById('guestList');
          //for loop - essentially this means start the variable i at 0
          //keep looping while i < names.length, and increment i by 1
          //each time we iterate through the loop.
          for(i=0;i < names.length; i++){
              //call the appendToList function above, and pass it names[i] and my list object
              //so essentially, loop over every name in the array and call that function with
              //it to add the name to the list.
              appendToList(names[i],list);
          }
      }
      
      var name=[]//全球范围
      函数storeVals()//声明用于存储输入的函数
      {
      //document.getElementById查找具有
      //对应的ID。在这种情况下,我得到的是对象
      //在每个输入框后面,并检索其值属性
      //(输入的文本)。我还将它们与
      //加号(+)运算符,并在它们之间添加带有“”的空格
      //我将所有这些结果存储在一个名为name的变量中。
      var name=document.getElementById('vorname').value+''+document.getElementById('nachname').value;
      //我正在访问上面声明的名称数组
      //使用从0开始计数的索引访问数组。
      //因此,要获得第一个元素,我需要使用名称[0]。
      //在本例中,我使用names.length而不是数字,因为它将返回
      //数组中当前的元素数。如果为零,则返回0。
      //当有一个元素时,它将返回1,它恰好是第二个元素的索引。
      //这意味着我总是给元素命名