当我单击按钮时,调用一个函数将返回一些值。我需要获取这些值并使用Jquery通过AJAX发送。
在click button函数下面给出。在此函数内部正在调用另一个函数(此处返回的值我需要通过ajax传递)
问题是我无法在ajax中发送这些值。
有人可以帮助我做到这一点吗?我不知道在哪里犯错。
var dataSetsave = [];
function saveschedule(startDate, endDate, periodInDays,cnt) {
//dataSetsave = [];
var numWorkDays = 0;
var currentDate = new Date(startDate);
while (numWorkDays < periodInDays && currentDate <= endDate) {
currentDate = currentDate.addDays(1);
// Skips friday
if (currentDate.getDay() !== 5) {
numWorkDays++;
}
if (numWorkDays == periodInDays) {
numWorkDays = 0;
cnt++;
let date = dateToYMD(currentDate);
let pos = holidays.indexOf(date);
if (pos != -1) {
//console.log("replace",date,"with",instead[pos])
date = instead[pos];
}
let treatment = pad(cnt) + nth(cnt) + (cnt == 1 ? " Basic" : " Control") + " Treatment"
dataSetsave.push([treatment, date])
}
}
return dataSetsave;
}
如何将dataSetsave.push([处理,日期])传递给ajax并传递给控制器php?
保存到数据库,例如
Date
2019-01-01
2019-01-02
2019-01-03 so on.......
该函数产生大量结果。我需要通过AJAX传递多行。
完整代码(点击按钮)
$('#saveactivityarea').on('click', function(event) { //save new activity area
$('#newModalFormactivityarea').validate({
rules: {
act_contractbranch_name: {
required: true
}
},
messages: {
act_contractbranch_name: {
required: "Required"
}
},
submitHandler: function(form) {
var act_contractbranch_name = $("#act_contractbranch_name").val();
var act_job_freq_contract = $("#act_job_freq_contract option:selected").val();
var act_city_activity = $("#act_city_activity option:selected").val();
var act_contractbranch_addr = $("#act_contractbranch_addr").val();
var act_contractbranch_contactname = $("#act_contractbranch_contactname").val();
var act_contractbranch_phone = $("#act_contractbranch_phone").val();
var act_contractbranch_emailid = $("#act_contractbranch_emailid").val();
var act_contractbranch_FAX = $("#act_contractbranch_FAX").val();
var fjd_date = $('#act_contractbranch_firstjobdt').val(); //24-06-2019
var act_contractbranch_firstjobdt = fjd_date.split("-").reverse().join("-");
var act_contractbranch_firstjd = new Date(fjd_date.split("-").reverse().join("-"));
var contractID = $('#contractID').val();
var confirstjobdt = $('.Contractend_firstjobdt').val();
var contractend_firstjobdt = new Date(confirstjobdt.split("-").reverse().join("-"));
var act_job_freq_daysbtw = $("#act_job_freq_contract option:selected").attr('data-freqdays');
var branch_pestorclean = $("#branch_pestorclean option:selected").val();
var branch_treatmenttype = $('#branch_treatmenttype').val();
var returnedValues = saveschedule(act_contractbranch_firstjd,contractend_firstjobdt,act_job_freq_daysbtw,0);
var contractID = $('#contractID').val();
$.ajax({
type: "POST",
url: 'activity_submitted',
data: {
getcontractbranch_name: act_contractbranch_name,
getcontractbranch_freq: act_job_freq_contract,
getbranch_city: act_city_activity,
getcontractbranch_addr: act_contractbranch_addr,
getcontractbranch_contactname: act_contractbranch_contactname,
getcontractbranch_phone: act_contractbranch_phone,
getcontractbranch_emailid: act_contractbranch_emailid,
getcontractbranch_FAX: act_contractbranch_FAX,
getcontractbranch_firstjobdt: act_contractbranch_firstjobdt,
getpest_or_clean: branch_pestorclean,
get_typeof_treatment: branch_treatmenttype,
getdataSetsave: returnedValues,
//getjob_schedule_frequency: job_schedule_frequency,
getcontract_id: contractID
},
success: function(data) {
//console.log(data);
var obj = JSON.parse(data);
} //end success
});
}
});
}); //save activity area
PHPMODAL功能
$data_jobschedule = array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $this->input->post('getschedule'),
//'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
'created_at' =>$created_Dt
);
答案 0 :(得分:0)
在saveschedule
功能的结尾处使用return。
return语句结束函数执行,并指定要返回给函数调用者的值。
因此,在函数末尾,只需返回dataSetsave
数组:
return dataSetsave;
然后...呼叫者是submitHandler
。因此,只需将重新调整的saveschedule
值分配给一个变量。喜欢:
returnedValues = saveschedule(...);
最后,在Ajax data
对象中使用它:
data: {
schedule: returnedValues,
getcontractbranch_name: act_contractbranch_name,
...