我有一个表单,其中一些字段是动态生成的。
<table class="insideform">
<tr>
<td>
<script>
$(document).ready(function() {
$('#addrange').click(function(){
var value = '<tr><td><input type="number" size="10" id="from" name="from[]" value=""></td>';
value += '<td><input type="text" size="10" id="to" name="to[]" value=""></td>';
value += '<td><input type="text" id="disprice" name="disprice[]" /></td>';
value += '<td valign="middle" id="removerange">x</td>';
value += '<td id="to_err" class="err"></td></tr>';
$('.discounttable').append(value);
});
$('body').on("click","#removerange",function(){
$(this).parent().remove();
});
$('#maxqty').change(function(){
var value = $('#maxqty').val() + " Above";
$('#maxabove').text(value);
});
});
</script>
<table class="discounttable">
<tr>
<th>From</th>
<th>To</th>
<th>Price</th>
</tr>
<tr>
<td colspan="2" align="right"><span id="maxabove">10 Above</span></td>
<td><input type="text" name="maxaboveinput" id="maxaboveinput" /></td>
<td id="maxaboveinput_err" class="err"></td>
</tr>
<tr>
<td><input type="text" size="10" id="from" name="from[]" value=""></td>
<td><input type="text" size="10" id="to" name="to[]" value=""></td>
<td><input type="text" /></td>
<td valign="middle" id="removerange">x</td>
<td id="to_err" class="err"></td>
</tr>
</table> <input type="button" name="addrange" id="addrange" value="Add Row"/>
我想验证codeignitor中的所有to [],from []字段,我使用ajax调用来验证表单这里是代码:
$('#submit').click(function(){
console.log($("#form").serialize());
$.ajax({
url:'<?php echo base_url(); ?>index.php/placeorder/valids',
type:'POST',
data:$("#form").serialize()
}).done(function(data){
$("#validations").html(data);});
我试图在控制器中编写此代码:
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_message('%s required', '*required');
$this->form_validation->set_rules('to[]', 'To field', 'required|xss_clean');
$this->form_validation->set_rules('from[]', 'From field', 'required|xss_clean');
$errors = array();
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
}
它没有验证我的字段......并且validation_errors没有显示任何我试图将它解析为json并回显json代码的东西......'to'和'from'在json中显示为空。有人可以帮忙吗?