jQuery:通过 SELECT 选项设置克隆数量
我有一个选择,我可以在其中选择我有多少个孩子,然后获取正确数量的输入。此片段克隆并添加数字。那么如果两次选择4,那么输入的数量就是8。我怎样才能使它成为固定数量的克隆,而不是添加克隆呢?
此外,我想为每个克隆添加 ID(我在脚本中将其称为“userInputID”,但它不起作用)或名称。我也这样做怎么办?
//Add the right amount inputs based on SELECT option clicked
$('select.linkedToNext').change(function() {
// Get number of children chosen
var childrenCount = $(this).val(),
// Get a reference to the first input group so we can clone it
userInputID = $(".linkedToPrev div").index();
birthday = $('.linkedToPrev div:first').attr('name', userInputID);
// Clone per number chosen
for (var i = 0; i < childrenCount; i++) {
// Insert clone after original.
// IRL, you probably need to do some preprocessing on it first.
birthday.clone().insertAfter(birthday);
$(this).parent().next(".linkedToPrev").slideDown('slow');
}
});
你们能帮我一下吗?我想我在这个问题上超出了我的想象......:-(
I have a SELECT where I choose how many children I have, and next gets the right amount of inputs. This snippit clones and adds the number. So if you choose 4 two times, the amount of inputs would be 8. How can I make it a fixed amount of clones, and not added clones?
Further more, I would like to add ID (I called it 'userInputID' in the script but it doesn't work) or NAMEs to each Clone. How do I do this as well?
//Add the right amount inputs based on SELECT option clicked
$('select.linkedToNext').change(function() {
// Get number of children chosen
var childrenCount = $(this).val(),
// Get a reference to the first input group so we can clone it
userInputID = $(".linkedToPrev div").index();
birthday = $('.linkedToPrev div:first').attr('name', userInputID);
// Clone per number chosen
for (var i = 0; i < childrenCount; i++) {
// Insert clone after original.
// IRL, you probably need to do some preprocessing on it first.
birthday.clone().insertAfter(birthday);
$(this).parent().next(".linkedToPrev").slideDown('slow');
}
});
Can you guys help me here? I guess I went over my head on this one... :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该将 var ChildrenCount = $(this).val() 更改为:
可以这样做:
First, you should change
var childrenCount = $(this).val()
to this:That can be done like this: