3 回答
TA贡献1852条经验 获得超1个赞
如果您使用 PHP 生成随机名称,则会在服务器上完成一次。然后您的 JS 代码复制相同的元素。你需要的是用js生成唯一的名字。
如果可以,请避免随机,理论上,您可以命中相同的数字并遇到神秘的错误。
var generateField = function(name)
{
return '<div><input type="text" name="'+name+'" value=""/><a href="javascript:void(0);" class="remove_button">Remove</a></div>'; //New input field html
}
//Once add button is clicked
$(addButton).click(function(){
//Check maximum number of input fields
if(x < maxField){
x++; //Increment field counter
$(wrapper).append(generateField('field_name['+x+']' ) ); //Add field html
}
});
TA贡献1804条经验 获得超8个赞
随机并不一定意味着唯一,即使碰撞极其罕见。这个解决方案只是增加一个totalFieldsCreated变量来获得下一个唯一数字(直到 JavaScript 可以提供的最大值。)
新字段是动态创建的,而不是使用固定的 HTML 字符串。(这种技术更灵活。)
$(document).ready(function() {
// Defines global identifiers
let
currentFieldCount = 1,
totalFieldsCreated = 1;
const
maxFieldCount = 100,
addButton = $('.add_button'),
wrapper = $('.field_wrapper');
// Calls `addField` when addButton is clicked
$(addButton).click(addField);
// Executes anonymous function when `Remove` is clicked, which removes
// the parent div, and decrements (and logs) `currentFieldCount`
$(wrapper).on('click', '.remove_button', function(e) {
e.preventDefault();
$(this).parent('div').remove();
currentFieldCount--;
console.log(`currentFieldCount: ${currentFieldCount}`);
});
// Defines the `addField` function
function addField(){
// Makes sure that `currentFieldCount` and `totalFieldsCreated`
// are not at maximum before proceeding
if(
currentFieldCount < maxFieldCount &&
totalFieldsCreated < Number.MAX_VALUE
){
// Creates an input element, increments `totalFieldsCreated`,
// and uses the incremented value in the input's `name` attribute
const input = document.createElement("input");
input.type = "text";
input.name = "field" + ++totalFieldsCreated;
input.value = "";
// Creates an anchor element with the `remove_button` class
const a = document.createElement("a");
a.href = "javascript:void(0);";
a.classList.add("remove_button");
a.title = "remove";
a.innerHTML = "Remove";
// Adds the new elements to the DOM, and increments `currentFieldCount`
const div = document.createElement("div");
div.appendChild(input);
div.appendChild(a);
$(wrapper).append(div);
currentFieldCount++;
// Logs the new values of both variables
console.log(
`currentFieldCount: ${currentFieldCount},`,
`totalFieldsCreated ${totalFieldsCreated}`
);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field_wrapper">
<div>
<input type="text" name="field1" value="" />
<a href="javascript:void(0);" class="add_button" title="Add field">Add More</a>
</div>
</div>
TA贡献1852条经验 获得超7个赞
Math.random()在 js 而不是rand()在 php 中尝试,Math.floor(Math.random()*90000) + 10000会生成一个五位数的随机数,希望这会有所帮助
$('.rand').attr('name',"fields["+Math.floor(Math.random()*90000) + 10000+"]")
$('.add_button').click(function(e){
$('.field_wrapper').append('<div><input type="text" name=fields['+Math.floor(Math.random()*90000) + 10000+'] value=""/><a href="javascript:void(0);" class="remove_button">Remove</a></div>')
})
$(document).on('click','.remove_button',function(e){
$(this).parent().remove()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="field_wrapper">
<div>
<input type="text" class="rand" value=""/>
<a href="javascript:void(0);" class="add_button" title="Add field">Add More</a>
</div>
</div>
- 3 回答
- 0 关注
- 153 浏览
添加回答
举报