0
我正在爲CakePHP
view
添加多個model
數據,我會讓用戶選擇他想要保存的行數。使用JavaScript重複模型的輸入文本不起作用
這個過程是如果我通過創建視圖的CakePHP的行,但如果我使用jQuery的數據不保存,我檢查的代碼之間的差異通過CakePHP的,並通過jQuery複製的代碼沒有看到一些重要的東西產生:
CakePHP的產生形式:
<div class="location-row">
<div class="field wrp">
<input name="data[Location][0][name]" value="Location name" maxlength="50" type="text" id="Location0Name">
</div>
<div class="value wrp">
<input name="data[Location][0][value]" value="Location value" maxlength="50" type="text" id="Location0Value">
</div>
</div>
<div class="location-row">
<div class="field wrp">
<input name="data[Location][1][name]" value="Location name" maxlength="50" type="text" id="Location1Name">
</div>
<div class="value wrp">
<input name="data[Location][1][value]" value="Location value" maxlength="50" type="text" id="Location1Value">
</div>
<div class="btn remove-location-row bad">delete</div>
</div>
<div class="location-row">
<div class="field wrp">
<input name="data[Location][2][name]" value="Location name" maxlength="50" type="text" id="Location2Name">
</div>
<div class="value wrp">
<input name="data[Location][2][value]" value="Location value" maxlength="50" type="text" id="Location2Value">
</div>
<div class="btn remove-location-row bad">delete</div>
</div>
保存過程可以完美兼容CakePHP的生成形式:
echo $this->Form->input('Comparison.0.name', array ('div' => array ('class' => 'field wrp'), 'label' => false, 'value' => $name_text));
echo $this->Form->input('Comparison.0.value', array ('div' => array ('class' => 'value wrp'), 'label' => false, 'value' => $value_text));
echo $this->Form->input('Comparison.1.name', array ('div' => array ('class' => 'field wrp'), 'label' => false, 'value' => $name_text));
echo $this->Form->input('Comparison.1.value', array ('div' => array ('class' => 'value wrp'), 'label' => false, 'value' => $value_text));
echo $this->Form->input('Comparison.2.name', array ('div' => array ('class' => 'field wrp'), 'label' => false, 'value' => $name_text));
echo $this->Form->input('Comparison.2.value', array ('div' => array ('class' => 'value wrp'), 'label' => false, 'value' => $value_text));
隨着CHRO我調試器我將所有與CakePHP
形式完全出爐的數據:
_method:POST
data[Location][0][name]:Location name
data[Location][0][value]:Location value
data[Location][1][name]:Location name
data[Location][1][value]:Location value
data[Location][2][name]:Location name
data[Location][2][value]:Location value
jQuery的重複行
<div class="location-row">
<div class="field wrp">
<input name="data[Location][0][name]" value="Location name" maxlength="50" type="text" id="Location0Name">
</div>
<div class="value wrp">
<input name="data[Location][0][value]" value="Location value" maxlength="50" type="text" id="Location0Value">
</div>
</div>
<!-- FROM HERE THE ROWS ARE DUPLICATED with jQuery -->
<div class="location-row">
<div class="field wrp">
<input name="data[Location][1][name]" value="Location name" maxlength="50" type="text" id="Location1Name">
</div>
<div class="value wrp">
<input name="data[Location][1][value]" value="Location value" maxlength="50" type="text" id="Location1Value">
</div>
</div>
<div class="location-row">
<div class="field wrp">
<input name="data[Location][2][name]" value="Location name" maxlength="50" type="text" id="Location2Name">
</div>
<div class="value wrp">
<input name="data[Location][2][value]" value="Location value" maxlength="50" type="text" id="Location2Value">
</div>
</div>
如果我複製只烤CakePHP
輸入文本發送與jQuery
行,但是HTML似乎到是確定:
_method:POST
data[Location][0][name]:Location name
data[Location][0][value]:Location value
這是我使用克隆輸入字段jQuery代碼:
var comparison_id = 1;
$('.add-location-row').click(function() {
if ($(this).siblings('.location-row').length > 1) {
$(this).siblings('.location-row').last().clone().insertAfter($('.location-row').last());
} else {
$(this).siblings('.location-row').last().clone().append('<div class="btn remove-location-row bad">delete</div>').insertAfter($('.location-row').last());
}
var element_1 = $(this).prev().find('div:nth-child(1) input');
var element_2 = $(this).prev().find('div:nth-child(2) input');
var id_att_1 = element_1.attr('id');
var id_att_2 = element_2.attr('id');
id_att_1 = id_att_1.replace((comparison_id-1).toString(), comparison_id.toString());
id_att_2 = id_att_2.replace((comparison_id-1).toString(), comparison_id.toString());
var name_att_1 = element_1.attr('name');
var name_att_2 = element_2.attr('name');
name_att_1 = name_att_1.replace((comparison_id-1).toString(), comparison_id.toString());
name_att_2 = name_att_2.replace((comparison_id-1).toString(), comparison_id.toString());
comparison_id ++;
element_1.attr('id', id_att_1);
element_2.attr('id', id_att_2);
element_1.attr('name', name_att_1);
element_2.attr('name', name_att_2);
$(this).prev().find('.remove-location-row').bind('click', function() {
$(this).parent().remove('.location-row');
});
});
使用jQuery對CakePHP
沒有影響,只有第一行被保存,我錯了?
沒有你看看網絡數據鍍鉻網片或螢火蟲等,如果後/獲得數據是從處理腳本中接收的? – 2012-04-21 23:19:09
我發佈瞭如何使用javascript複製輸入表單元素的數據。 – vitto 2012-04-22 22:24:00
我記得你的問題中有一些jQuery代碼。你爲什麼刪除它們?你使用.clone()來複制行嗎? – 2012-04-22 23:33:21