gpt4 book ai didi

javascript - CakePHP 无法读取 javascript 生成的克隆表单中的正确输入名称

转载 作者:行者123 更新时间:2023-11-28 07:03:06 25 4
gpt4 key购买 nike

我已经实现了一个扩展表单函数,该函数还在克隆表单时生成唯一的字段名称。

由于 js 的工作方式,扩展表单基础 (#readroot) 中的字段名称与 Cake 不兼容,直到脚本运行并更正它们为止。

基础名称:年龄、年级、学校
js运行后的样子:

数据[学生][1][年龄]

数据[学生][1][年级]

数据[学生][1][学校]

所以我希望 $this->request->data 会收到这样的数组:

Array ( 
[Post] => Array (
[title] => Mr
[contact_person] => Sam
[home_tel] => 1234567
[mobile] => 1234567
[email] => email@gmail.com
[relationship] => family
[frequency] => once per week
[duration] => 1hr
[user_id] => 1
)
[Student] => Array (
[0] => Array (
[age] => 10
[grade] => 1
[gender] => 1
[school] => ABC
)
// This is where the extend form starts.
[1] => Array (
[age] => 11
[grade] => 2
[gender] => 0
[school] => ABC2
)
)
)

但是,$this->request->data 读取的是:

Array ( 
[age] => 11
[grade] => 2
[gender] => 0
[school] => ABC2
[Post] => Array (
[title] => Mr
[contact_person] => Sam
[home_tel] => 1234567
[mobile] => 1234567
[email] => email@gmail.com
[relationship] => family
[frequency] => once per week
[duration] => 1hr
[user_id] => 1
)
[Student] => Array (
[0] => Array (
[age] => 10
[grade] => 1
[gender] => 1
[school] => ABC
)
) )

看起来它读取了更改之前的字段名称。我是 Javascript 新手,希望有人能拯救我......

表单 html 和 javascript 的简化版本:

<?php 
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('contact_person');
echo $this->Form->input('home_tel');
echo $this->Form->input('mobile');
echo $this->Form->input('email');
echo $this->Form->input('relationship');
echo $this->Form->input('Student.0.age');
echo $this->Form->input('Student.0.grade');
echo $this->Form->input('Student.0.school');
?>
<!-- Extend Form Reference -->
<span id="readroot" style="display: none">
<input class="btn btn-default" type="button" value="Remove review" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
<?php
echo $this->Form->input('Student.1.age', array(
'name' => 'age',
);
echo $this->Form->input('Student.1.grade', array(
'name' => 'grade',
'options' => array(
'1' => __('Grade 1'),
'2' => __('Grade 2')
)
));
echo $this->Form->input('Student.1.school', array(
'name' => 'school'
));
?>
</span>
<!-- Extend Form Reference End-->
<span id="writeroot"></span>
<input class="btn btn-default" type="button" onclick="moreFields()" value="Give me more fields!" />
<script>
var counter = 1;
function moreFields() {
counter++;
var newField = document.getElementById('readroot').cloneNode(true);
newField.id = '';
newField.style.display = 'block';
var newFields = newField.querySelectorAll('[name]');
for (var i=0;i<newFields.length;i++) {
var theNames = newFields[i].name
if (theNames)
newFields[i].name = "data[Student][" + counter + "][" + theNames + "]";

}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newField,insertHere);
}
//window.onload = moreFields;
</script>
<?php echo $this->Form->end(); ?>

最佳答案

由于我设法使其工作,但不是以一种优雅的方式,我将其作为答案发布。

    public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$this->request->data['Post']['user_id'] = AuthComponent::user('id');
unset($this->Post->Student->validate['post_id']);
$a = $this->request->data;
$b = array_splice($a, 4);
if ($this->Post->saveAssociated($b)) {
$this->Session->setFlash(__('The post has been saved.'), 'alert_box', array('class' => 'alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'alert_box', array('class' => 'alert-danger'));
}
}
$users = $this->Post->User->find('list', array('order' => array('User.id' => 'asc')));
$subjects = $this->Post->Subject->find('list');
$this->set(compact('users', 'subjects'));
}

前面有 4 个值,而数组的其余部分正是我想要的,我基本上只是将其切掉并传递其余部分来保存..

关于javascript - CakePHP 无法读取 javascript 生成的克隆表单中的正确输入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com