2017-02-27 35 views
0

我试图每次使用data-name提交姓名时都要跟踪。我如何提醒用户让他们知道内容是否已发布到网站上,以及提交了哪些其他名称。如何跟踪所有已提交的输入

<div id="container"> 
    <form method="POST" action="#"> 
    <label class="large">Please enter your name 
     <input id="textbox" class="name" data-name="" pattern="[a-zA-Z0-9]{1-45}"> 
    </label> 
    </form> 
</div> 

这是JQuery的我使用的是:

//When the form is submitted 
$("#form").submit(function(){ 
    //get the value of the input 
    var input = $('#textbox').val(); 

    //insert it into the data-name attribute 
    var name = $("#textbox").attr("data-name", input); 

    //Aler the user 
    $("input[data-name]").val(function(){ 
    alert($(this).data('name')); 
    }); 

}); 
+0

如何在数据得到持续?提交表单通常涉及页面刷新或重定向,因此您需要通过某种服务器端代码更新值。另外,我不确定你为什么在'val()'中使用函数。你应该使用'$(“input [data-name]”).val(name)',但是由于你刚刚从$('#textbox')中取出了值,所以有点令人困惑。所以整个代码是没有操作的。 –

回答

0

我认为你的jQuery选择不正确。

$("#form")表示它正在定位一个id为“form”的元素。为了做到这一点,你需要给你的表单元素一个表单ID。例如。 <form id="form" method="POST" action="#">

但是,为了提醒文本框的值,您不需要将该值放入数据名称属性中,而是在提交时将值从表单中提取出来。

$('#myForm').submit(function(e){ 
    alert(e.target.foo.value); 
}) 

请参阅下小提琴,对于一个例子:

https://jsfiddle.net/dgw3bw63/

+0

谢谢,但可以输出已提交的所有以前的输入吗? – Lawl

+0

当然,将它们存储在一个数组中! https://jsfiddle.net/dgw3bw63/1/ – derp