表单内的 jQuery clone() 问题
我正在 CakePHP 中使用 jQuery 制作一个表单。我的问题是我需要能够使用 jQuery 克隆表单的某些字段。该表格是预订表格,我需要克隆客户详细信息以添加超过 1 个客户(如果是团体)。
我正在使用这段代码:
var idx = 0;
$(document).ready(function() {
$("#addCustomerFields").click(function() {
idx = idx + 1;
var x = $("#Cliente").clone();
$(x)
.attr("id", "Client." + idx)
.find("label").each(function() {
$(this).attr("for", $(this).attr("for").replace(".0", "." + idx));
})
.end()
.find("input").each(function() {
$(this)
.attr("id", $(this).attr("id").replace(".0", "." + idx))
.attr("name", $(this).attr("name").replace("[0]", "[" + idx + "]"));
//this.value = '';
})
$("#CustomerFields").append(x);
});
});
它工作正常,但仅限于文本输入字段。下拉菜单(选择)不起作用。看起来 select 的属性(id 和 name)没有被脚本更改。当我在数据库中添加 2 个客户时,第一个客户是男性,第二个客户是女性,第一个客户显示为女性,第二个客户显示为无性别。每个字段都有其名称和 ID:客户端[0][名字]、客户端[0][性别]等。当我克隆时,我会增加数字,并且仅在文本输入中起作用。
观点是:
<fieldset id="Cliente">
<legend class="legend"><?php __('Client Info'); ?></legend>
<?php
//this is select
echo $this->Form->input('Client.0.sex_id', array(
'label'=>'Gender',
'div'=>'float IconSex',
)
);
echo $this->Form->input('Client.0.firstname', array(
'label'=>'First name',
'div'=>'float IconUser'
)
);
echo $this->Form->input('Client.0.lastname', array(
'label'=>'Last name',
'div'=>'float IconUser'
)
);
//another select
echo $this->Form->input('Client.0.country_id', array(
'label'=>'Country',
'div'=>'clear IconCountry',
'default'=>'121'
)
);
?>
</fieldset><div id="CustomerFields"></div>
<?php
echo $this->Html->div('IconAdd', 'Add customer.', array(
'id'=>'addCustomerFields',
)
);
?>
有什么想法吗?
I am making a form in CakePHP with jQuery. My problem is that I need to be able to clone some fields of the form with jQuery. The form is a reservation and I need to clone customer details to add more than 1 customer (if it is a group).
I am using this code:
var idx = 0;
$(document).ready(function() {
$("#addCustomerFields").click(function() {
idx = idx + 1;
var x = $("#Cliente").clone();
$(x)
.attr("id", "Client." + idx)
.find("label").each(function() {
$(this).attr("for", $(this).attr("for").replace(".0", "." + idx));
})
.end()
.find("input").each(function() {
$(this)
.attr("id", $(this).attr("id").replace(".0", "." + idx))
.attr("name", $(this).attr("name").replace("[0]", "[" + idx + "]"));
//this.value = '';
})
$("#CustomerFields").append(x);
});
});
It works OK but only with text input fields. Drop downs (select) don't work. It looks like the attributes of select (id and name) are not changed by the script. When I add 2 customers, first one male and the second one female in the database the first one appears as female and second is without sex. Each field has its name and id: Client[0][Firstname], Client[0][Sex] and etc. When I clone I increase the number and this wokrs only in text inputs.
The view is:
<fieldset id="Cliente">
<legend class="legend"><?php __('Client Info'); ?></legend>
<?php
//this is select
echo $this->Form->input('Client.0.sex_id', array(
'label'=>'Gender',
'div'=>'float IconSex',
)
);
echo $this->Form->input('Client.0.firstname', array(
'label'=>'First name',
'div'=>'float IconUser'
)
);
echo $this->Form->input('Client.0.lastname', array(
'label'=>'Last name',
'div'=>'float IconUser'
)
);
//another select
echo $this->Form->input('Client.0.country_id', array(
'label'=>'Country',
'div'=>'clear IconCountry',
'default'=>'121'
)
);
?>
</fieldset><div id="CustomerFields"></div>
<?php
echo $this->Html->div('IconAdd', 'Add customer.', array(
'id'=>'addCustomerFields',
)
);
?>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅使用当前代码
.find("input").each(function(...
尝试
.find(":input")
到 < a href="http://api.jquery.com/input-selector/" rel="nofollow">匹配所有输入(选择、输入、文本区域等)或仅使用
$('select , input')
匹配输入和选择
You are only selecting input elements with your current code
.find("input").each(function(...
Try
.find(":input")
to match all inputs (selects, inputs, textarea etc)or just use
$('select, input')
to match inputs and selects