简体   繁体   English

使用用户输入值将HTML表单XML保存到javascript变量?

[英]Saving HTML form XML with user input values to javascript variable?

Currently right now I have a form rendering from a database from JSON. 目前我现在有一个来自JSON数据库的表单呈现。 What I want to do is store the Form html with the input values into a javascript variable. 我想做的是将带有输入值的Form html存储到javascript变量中。 Is there anyway that this would be possible? 无论如何,这有可能吗?

Here would be a simple example.. 这将是一个简单的例子..

<form method="post">
<input type="text">
<input type="submit" value="Submit">
<button 
</form>

So the user would input the word "test". 因此,用户将输入单词“ test”。 I then would want to store the following 然后我想存储以下内容

<form method="post">
<input type="text" value="test">
<input type="submit" value="Submit">
<button 
</form>

Would there be a way of doing this? 有没有办法做到这一点?

You can manually build the string one piece at a time like this based on your form: 您可以根据自己的形式,一次一次手动构建字符串:

<form>  
  <input id="input1" type="text">
  <input id="input2" type="text">
  <input id="input3" type="text">
  <input type="submit" id="submit" value="Submit">
</form>

jQuery: jQuery的:

$( document ).ready(function() {

  $("#submit").click(function(){
   var inputval1 = $("#input1").val();
   var inputval2 = $("#input2").val();
   var inputval3 = $("#input3").val();
     var value = '<form><input id="input1" type="text" value="'+inputval1+'" >';
         value += '<input id="input2" type="text" value="'+inputval2+'" >';
         value += '<input id="input3" type="text" value="'+inputval3+'" >';
         value += '<input type="submit" id="submit" value="Submit">';
         value += '</form>';
     alert(value);  
   })  

});

Fiddle: https://jsfiddle.net/rdawkins/eusrhy47/21/ 小提琴: https //jsfiddle.net/rdawkins/eusrhy47/21/

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

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