Javascript 使用Angular js提交后重置表单
我想在提交表单后重置表单字段。我的代码工作正常,但这里是为了清除输入字段,我这样写的所有复选框字段我必须写的所有复选框字段Javascript 使用Angular js提交后重置表单,javascript,jquery,angularjs,forms,reset,Javascript,Jquery,Angularjs,Forms,Reset,我想在提交表单后重置表单字段。我的代码工作正常,但这里是为了清除输入字段,我这样写的所有复选框字段我必须写的所有复选框字段 $scope.appointmentForm = function() { $scope.formData = {}; $http({ method: "POST", url: "appointment_mail.php", data: { date: $scope.appt_date, fname: $scope.f
$scope.appointmentForm = function() {
$scope.formData = {};
$http({
method: "POST",
url: "appointment_mail.php",
data: {
date: $scope.appt_date,
fname: $scope.fname,
lname: $scope.lname,
email: $scope.email,
notes: $scope.notes,
services: $scope.services,
category: $scope.category,
address: $scope.address,
timings: $scope.appt_timings,
phone: $scope.phone,
services: $scope.services,
category: $scope.category
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
data = response.data;
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorFname = data.errors.fname;
$scope.errorLname = data.errors.lname;
$scope.errorMail = data.errors.email;
$scope.errorPhone = data.errors.phone;
$scope.errorNotes = data.errors.notes;
$scope.errorAddress = data.errors.address;
$scope.messages = null;
} else {
// if successful, bind success message to message
$scope.fname = '';
$scope.lname = '';
$scope.email = '';
$scope.note = '';
$scope.address = '';
$scope.phone = '';
$scope.appointment_form.$setPristine();
$scope.messages = data.messages;
$timeout(function() {
// Loadind done here - Show message for 3 more seconds.
$timeout(function() {
$scope.messages = false;
}, 3000);
}, 2000);
//From here is there any other alternative way
$scope.category = {
"red": "",
"blue": "",
"green": "",
}
$scope.services = {
"abc": "",
"xyz": "",
}
$scope.appt_timings = "";
$scope.notes = "";
$("#iva_apptdate").datepicker('setDate', null);
}
}, function(error) {});
}
});
<div class="check-wrap">
<div class="cat">
<span class="checkbox">
<input type="checkbox" name="category" checked data-ng-model="iva_category.red">
<label>Red
</label>
</span>
</div>
<div class="cat">
<span class="checkbox">
<input type="checkbox" name="category" data-ng-model="category.blue">
<label>Blue
</label>
</span>
</div>
<div class="cat">
<span class="checkbox">
<input type="checkbox" name="category" data-ng-model="category.green">
<label>Green/label>
</span>
</div>
</div>
$scope.appointmentForm=function(){
$scope.formData={};
$http({
方法:“张贴”,
url:“约会邮件.php”,
数据:{
日期:$scope.appt_日期,
fname:$scope.fname,
lname:$scope.lname,
电子邮件:$scope.email,
注:$scope.notes,
服务:$scope.services,
类别:$scope.category,
地址:$scope.address,
计时:$scope.appt_计时,
电话:$scope.phone,
服务:$scope.services,
类别:$scope.category
},
标题:{
“内容类型”:“应用程序/x-www-form-urlencoded”
}
}).然后(功能(响应){
data=response.data;
如果(!data.success){
//如果未成功,请将错误绑定到错误变量
$scope.errorFname=data.errors.fname;
$scope.errorLname=data.errors.lname;
$scope.errorMail=data.errors.email;
$scope.errorPhone=data.errors.phone;
$scope.errorNotes=data.errors.notes;
$scope.errorAddress=data.errors.address;
$scope.messages=null;
}否则{
//如果成功,则将成功消息绑定到消息
$scope.fname='';
$scope.lname='';
$scope.email='';
$scope.note='';
$scope.address='';
$scope.phone='';
$scope.appointment_form.$setPristine();
$scope.messages=data.messages;
$timeout(函数(){
//Loadind在此完成-显示消息3秒以上。
$timeout(函数(){
$scope.messages=false;
}, 3000);
}, 2000);
//从这里还有其他的选择吗
$scope.category={
“红色”:“,
“蓝色”:“,
“绿色”:“,
}
$scope.services={
“abc”:“,
“xyz”:“,
}
$scope.appt_timings=“”;
$scope.notes=“”;
$(“#iva_apptdate”).datepicker('setDate',null);
}
},函数(错误){});
}
});
红色
蓝色
绿色/标签>
我的问题是是否有其他方法可以做到这一点,因为在这里我必须编写更多的代码来清除复选框输入字段。如果有任何错误,请随时告知。这应该很有用。您应该将表单包含在
表单
标记中,然后使用以下内容:。这里有一个重置功能的示例。请不要使用下面答案中建议的jQuery或DOM操作。