在我使用jquery之前我的视图代码:
<form name="add_book_form" action="Backend/BookManage/save" method="POST">
<table width="100%" border="0" class="table" id="center" align="center">
<tr class="author_div_1">
<td width="5%"> </td>
<td>Author</td>
<td>
<select name="author_1" class="form-control">
<?php
foreach ($authorList as $author)
{
echo '<option value = "' . $author["id"] . '">' . $author["name"] . ' ' . $author["surname"] .'</option>';
}
?>
</select>
</td>
<td>
<div class="btn glyphicon glyphicon-plus btn-sm" id="add_more_author"></div>
</td>
</tr>
<tr>
<td height="68" colspan="4" align="center">
<input type="submit" id="submit_0" value="Save" class="btn btn-primary"/>
<input type="reset" value="Reset" class="btn btn-danger"/>
</td>
</tr>
</table>
</form>
脚本
var count = 1;
var cloned;
$(document).on("click", "#add_more_author", function() {
cloneData = $('.author_div_'+count).clone().attr('class','author_div_'+(++count));
cloneData.find('.form-control').attr('name','author_'+count);
cloneData.insertAfter('.author_div_'+ (count-1));
$('.author_count').attr('value',count);
});
点击add_more_author后,我的html源代码如下:
<select name="author_2" class="form-control">
<option value="1">First Value</option>
<option value="2">Second Value</option>
<option value="3">Third value</option>
</select>
<select name="author_3" class="form-control">
<option value="1">First Value</option>
<option value="2">Second Value</option>
<option value="3">Third value</option>
</select>
当我点击add_more_author并选择值然后提交它, 在我的控制器上我回显了POST值,First(author_1)就好了,但另一个我无法得到它。
echo 'AUTHOR 1 ' . $this->input->post('author_1') . '<br>';
echo 'AUTHOR 2 ' . $this->input->post('author_2');
结果:
AUTHOR 1 1
作者2