我使用 $.each 通过 ajax 从 php 获取数据,但是当我使用它时,我的浏览器说这个错误 Uncaught TypeError: Cannot read property 'length' of null,找不到任何答案。
$('#search-button-applicant').click(function (){
date_to = Date.parse($('#search-to-date').val());
date_from = Date.parse($('#search-from-date').val());
applicant_name = $('#search-applicant-name').val();
if($('#search-to-date').val() == "" && $('#search-from-date').val() == "" && applicant_name == "" )
{
alert("you can not search");
}
else if($('#search-to-date').val() != "" && $('#search-from-date').val() == "" || $('#search-to-date').val() == "" && $('#search-from-date').val() != ""){
alert("you can not search");
}
else{
var date_to_val = $('#search-to-date').val();
var date_from_val = $('#search-to-date').val();
var loan_type_val = $('#loan-type').val();
var search_word_val = $('#search-applicant-name').val();
$.ajax({
type: "post",
url: "filter_search_window_get.php",
dataType:'json',
data: {'func_num':loan_type_val,'Date_To':date_to_val,'Date_From':date_from_val,'search_word':search_word_val},
success: function(data){
$.each(data, function(i, item) {
$('#search-table-content-list').append("<tr><td style='width:7.5%;'><label>Date Filled</label></td><td style='width:9.5%;'><label>Date Released</label></td><td style='width:8%;'><label>"+data[i].last_name+"</label></td><td style='width:25%;'><label>Name</label></td><td style='width:7.5%;'><label>Term</label></td><td style='width:7.5%;'><label>Rate</label></td><td style='width:7.5%;'><label>Principal</label></td><td style='width:7.5%;'><label>Interest</label></td><td style='width:7.5%;'><label>Total</label></td></tr>");
});
}
});
}
});
php:
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
$loan_type =$_POST['loan_type_val'];
switch ($loan_type){
case '1':
tuition_get();
break;
case '2':
salary_get();
break;
case '3':
sss_get();
break;
case '4':
ofw_get();
break;
case '5':
business_get();
break;
}
function tuition_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
$query = "SELECT
a.applicant_id,
a.last_name,
a.first_name,
a.created_date,
b.form_type,
b.form_id,
c.stat,
d.release_date,
f.payment_term,
j.form_rate,
k.loan_amt,
k.interest_amt,
k.total_amt
FROM
applicant a
LEFT JOIN form_type b ON (a.form_id = b.form_id)
LEFT JOIN form_status c ON (c.status_id = a.status_id)
LEFT JOIN applicant_loan_repayment d ON (d.applicant_id = a.applicant_id)
LEFT JOIN applicant_tuition_loan_dtl f ON (f.applicant_id = a.applicant_id)
LEFT JOIN form_term j ON (j.form_id = a.form_id)
LEFT JOIN applicant_loan_repayment k ON (k.applicant_id = a.applicant_id)
WHERE a.last_name LIKE '%$search_word%' OR a.first_name LIKE '%$search_word%' ";
$result=mysql_query($$query)or die(mysql_error());
$data=array();
while ($row = mysql_fetch_array($result))
{
$data[] = array(
'last_name'=>$row['last_name'],
'first_name'=>$row['first_name'],
'form_typ'=>$row['form_type']
);
}
echo json_encode($data);
}
function salary_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function sss_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function ofw_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function business_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
我使用 $.each 从查询中获取所有获取数据并使用 ajax 将其发布到表中,但我的 $.each 无法正常工作,我不知道我的代码有什么问题。