0
我有問題與驗證提交按鈕。 所以在這裏我的代碼:如何解析表單[0] .submit不是函數(...)
var handleValidation = function() {
var form = $('#form-date');
var error = $('.alert-danger', form);
var success = $('.alert-success', form);
form.validate({
errorElement: 'span', //default input error message container
errorClass: 'help-block help-block-error', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "", // validate all fields including form hidden input
rules: {
start: {
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_date')?>",
type: "post",
data: {
start: function(){ return $("#date").val(); }
}
}
},
end: {
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_end')?>",
type: "post",
data: {
end: function(){ return $("#end").val(); }
}
}
},
},
messages: {
start:
{
remote: 'Date already in use.'
},
end:
{
remote: 'Date already in use.'
},
},
invalidHandler: function (event, validator) { //display error alert on form submit
success.hide();
error.show();
App.scrollTo(error, -200);
},
highlight: function (element) { // hightlight error inputs
$(element)
.closest('.form-group').addClass('has-error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change done by hightlight
$(element)
.closest('.form-group').removeClass('has-error'); // set error class to the control group
},
success: function (label) {
label
.closest('.form-group').removeClass('has-error'); // set success class to the control group
},
submitHandler: function (form) {
success.show();
error.hide();
form[0].submit(); // submit the form
}
});
}
<form class="form-horizontal" id="form-date" role="form" method="post" action="<?php echo base_url(); ?>activity/add">
<div class="form-body">
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
You have some form errors. Please check below.
</div>
<div class="alert alert-success display-hide">
<button class="close" data-close="alert"></button>
Your form validation is successful!
</div>
<input type="hidden" class="form-control" name="nik" value="<?php echo $this->session->userdata('nik')?>">
<div class="form-group">
<label class="col-sm-3 control-label">Start Date:<span
class="required">*</span> </label>
<div class="col-sm-7">
<div class="input-group date date-picker" data-date-format="yyyy-mm-dd">
<input type="text" class="form-control" readonly id="date" name="start" required>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span></div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">End Date:<span
class="required">*</span> </label>
<div class="col-sm-7">
<div class="input-group date date-picker" data-date-format="yyyy-mm-dd">
<input type="text" class="form-control" readonly id="end" name="end" required>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span></div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-actions">
<div class="row">
<div class="col-md-offset-3 col-md-5">
<button type="submit" class="btn green">
<i class="fa fa-pencil"></i> Create</button>
<button type="button" class="btn default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
我已經驗證,其中數據是空的顯示警告,但如果數據succesful.only顯示警告成功並沒有redirect.in控制檯錯誤顯示
形式[0] .submit不是函數(...)
所以其中錯代碼。 如何解決?
我認爲它應該因爲你有'var form = $('#form-date');',並且你的表單是'
m-date「...」,那麼'form [0]'是無效的,因爲它應該是'form'。 – Sean
感謝大家,它的工作。 – ione