jQuery-变量未更新到另一个变量
首先,我不是任何类型的程序员,所以我需要您解决我的问题。
我有一个触点表格,该表格在一个地方可以添加更多输入字段,直到最多10。 我添加的每个字段都在代码中具有我称为“ PERS”的类,并且我希望此类PERS在附近的增量数字,如pers1、2、3,然后继续进行。
获得增量值很容易,但问题是“ PERS”类不会更新变量,使文档负载的第一个数字保持。
这是代码,
<script type="text/javascript">
window.s = 1;
</script>
<script type="text/javascript">
$(document).ready(function() {
var addButton = $('.add_button');
var wrapper = $('.field_wrapper');
var maxField = 10;
var fieldHTML = '<div style="margin-top:5px;"><input type="text3" name="pers' + window.s + '" placeholder="Nome e Cognome"/><input type="time" style=margin-left:3.5px; autocomplete="off"><input type="time" style=margin-left:3.5px; autocomplete="off"><a href="javascript:void(0);"class="remove_button" title="Rimuovi"><img src="IMG/less_icon.png"></a></div>';
$(addButton).click(function() {
if (window.s < maxField) {
window.s++; //Increment field counter
$(wrapper).append(fieldHTML); //Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e) {
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
s--; //Decrement field counter
});
});
</script>
有一个全局变量“ window.s”,因为这是我最后一次尝试更新var。
这些字段正确地添加到应有的时间为10,但是“窗口”或仅在“ s”时仍然保持其第一个值1的值,因此“ pers”始终是“ pers1”。我认为暴露全局变量,我会解决这个问题,但什么也没有。
我在做什么错?
非常感谢您的帮助。
first of all i'm not a programmer of any kind, so i please need you to fix my issue.
I have a contact form which have inside a place where i can add more input fields till a maximum of 10.
Each field i add it has inside the code a class that i called "pers", and i want that this class pers has an incremental number near to it, as pers1, 2, 3 and so go on.
Getting the incremental value was easy but the problem is that the class "pers" wont update the variable keeping the first number on document load.
Here is the code
<script type="text/javascript">
window.s = 1;
</script>
<script type="text/javascript">
$(document).ready(function() {
var addButton = $('.add_button');
var wrapper = $('.field_wrapper');
var maxField = 10;
var fieldHTML = '<div style="margin-top:5px;"><input type="text3" name="pers' + window.s + '" placeholder="Nome e Cognome"/><input type="time" style=margin-left:3.5px; autocomplete="off"><input type="time" style=margin-left:3.5px; autocomplete="off"><a href="javascript:void(0);"class="remove_button" title="Rimuovi"><img src="IMG/less_icon.png"></a></div>';
$(addButton).click(function() {
if (window.s < maxField) {
window.s++; //Increment field counter
$(wrapper).append(fieldHTML); //Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e) {
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
s--; //Decrement field counter
});
});
</script>
There is a global variable "window.s" because it was my last try to get the var updated.
The fields adds correctly till 10 as it should be, but the "window.s" or when it was only "s" still keeps his first value of 1, and so "pers" is always "pers1". I thought that exposing a global variable i would have fix the problem, but nothing.
What am i doing wrong?
Thank you very much for your help guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码的问题是,当页面加载时,您仅设置
fieldhtml
一次。此时,window.s
是1
,因此这是每次参考fieldhtml
时使用的值。快速解决的方法是将fieldHtml
定义放置在consic> click()
事件处理程序中。但是,更好的方法是完全消除对增量变量的需求。在您动态生成的所有字段上使用相同的
名称
。这样,您就不需要维护计数(例如,如果添加了5个,并且有人删除了第三个,那么您当前最终会获得2per5
元素)。因此,它也简化了JS逻辑。此外,您应该将HTML放在A&lt; template&gt;
元素中,而不是JS中,以免代码库的交叉污染。这是我提到的变化的一个工作示例:
然后,您可以将所有
input
值接收为PHP代码中的数组像这样的phs-in-php“> 。The problem with your code is because you only set
fieldHTML
once, when the page loads. At this pointwindow.s
is1
, so this is the value that's used every time you referencefieldHTML
. The quick fix to this would be to put thefieldHTML
definition inside theclick()
event handler.However the better approach it to entirely remove the need for the incremental variable at all. Use the same
name
on all the fields you dynamically generate. This way you don't need to maintain the count (eg. if there have been 5 added and someone deletes the 3rd one, you'll currently end up with 2per5
elements). Because of this it also simplifies the JS logic. In addition you should put the HTML to clone in a<template>
element, not the JS, so that there's no cross-contamination of the codebase.Here's a working example of the changes I mention:
You can then receive all the
input
values as an array in your PHP code, like this.这是因为您没有在那里使用更新的值。
首先,您将一个值分配给
window.s
,然后创建一个使用window.s
的变量,从初始状态中,并且在每个添加的情况下,只需附加相同的fieldhtml
。因此,您获得了相同的价值。这是答案,您正在寻找:
It's because you have not used the updated value there.
First, you assign a value to
window.s
, and then you create a variable that uses the value ofwindow.s
from the initial state, and on each addition, it just appends the samefieldHtml
. So, you are getting the same value.Here is the answer, you are looking: