2017-04-12 26 views
-1

我在网页上有一个表单,我希望用户能够填写,点击提交,并显示类似“用户:[名称]有[事件]事件在[位置]的详细信息[描述]“在下面的评论部分。所以多个条目只会加载在对方之下。现在,当我点击提交时,它只会提交描述文本,而不会提交其他内容。我的函数getInfo()应该显示多个值,但不是。我该如何解决这个问题。完整的代码链接的下面提交表单没有连接到多个字段

https://github.com/tayrembos/Nav/blob/master/back.html

   <script type="text/javascript"> 
       function getInfo() { 
        text = name.value; 
        text = words.value; 
        document.getElementById("para").innerHTML += '<p>'+ text 
        document.getElementById("words").value = "Enter comment" 
        document.getElementById('name').value = "Enter name" 
       } 
       </script> 

       <form method="POST" name='myform'> 
       <p>Enter your name: 
        <textarea id='name' rows="1" cols="20">Enter name</textarea> 

       <textarea id='name' rows="1" cols="20">Enter name</textarea> 

       <textarea id='words' rows="10" cols="20">Enter comment</textarea> 
       <input type="button" onclick="getInfo()" value="Submit!" /> <br> 
       <p id="para"></p> 

回答

0

我追加使用jQuery的从(投票是否真的解决您的问题)。

function myFunction() { 
 

 
    var x = document.getElementById("product"); 
 
    var txt = ""; 
 
    var all = {}; 
 
    var i; 
 

 
    for (i = 0; i<x.length-1; i++) { 
 

 
      //txt = txt + x.elements[i].value + "<br>"; 
 
      all[x.elements[i].name]= x.elements[i].value; 
 

 
    } 
 

 
$("p").append(JSON.stringify(all, null, 2)); 
 

 
    //var myObj = { "name":"John", "age":31, "city":"New York" }; 
 

 
    //document.getElementById("demothree").innerHTML = myObj; 
 

 
    //var myJSON = JSON.stringify(all); 
 
    //window.location = "server.php?x=" + myJSON; 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
 
<form id="product"> 
 
    
 
    Expire: <input type="text" name="pexpire" value="3:45"><br> 
 
    Old Price: <input type="text" name="poldprice" value="30"><br> 
 
    Price: <input type="text" name="pprice" value="28"><br> 
 
    Category: <input type="text" name="pcategory" value="Ενδύματα"><br> 
 
    Variaty: <input type="text" name="pvariaty" value="Τζιν"><br> 
 
    City: <input type="text" name="pcity" value="Δράμα"><br> 
 
    Store: <input type="text" name="pstore" value="Groove"><br> 
 
    Picture: <input type="text" name="ppicture" value="aaa"><br> 
 

 
    
 

 
</form> 
 

 
<button onclick="myFunction()">Submit</button> 
 

 
<p id="list"></p>