我有一个jQuery代码来检查是否存在具有特定id的html元素。我编写了现有和不存在的html元素的if和else子句。但是当元素存在时为什么jQuery会将值返回为undefined?
您可以在下面看到jQuery代码。定义变量start_time,因为它进入子句条件但是将值检测为undefined。代码有什么问题?
var count = 0;
for(i=1; i<=n_day; i++){
count = count + 1 ;
var start_time = jQuery("#txtStartTime_detail"+i);
var end_time = jQuery("#txtEndTime_detail"+i);
var start_location = jQuery("#txtStartLocation_detail"+i);
var end_location = jQuery("#txtEndLocation_detail"+i);
var start_location_coor = jQuery('#txtStartLocation_Coordinates_detail'+i);
var end_location_coor = jQuery('#txtEndLocation_Coordinates_detail'+i);
//var day = jQuery('#txtDay'+i);
if(typeof start_time == 'undefined'){
start_time = '';
}else{
start_time = jQuery("#txtStartTime_detail"+i).val();
alert(start_time);
}
if(typeof end_time == 'undefined'){
end_time = '';
}else{
end_time = jQuery("#txtEndTime_detail"+i).val();
}
if(typeof start_location == 'undefined'){
start_location = '';
}else{
start_location = jQuery("#txtStartLocation_detail"+i).val();
}
if(typeof end_location == 'undefined'){
end_location = '';
}else{
end_location = jQuery("#txtEndLocation_detail"+i).val();
}
if(typeof start_location_coor == 'undefined'){
start_location_coor = '';
}else{
start_location_coor = jQuery('#txtStartLocation_Coordinates_detail'+i).val();
}
if(typeof end_location_coor == 'undefined'){
end_location_coor = '';
}else{
end_location_coor = jQuery('#txtEndLocation_Coordinates_detail'+i).val();
}
requestdetail += '<div class="yellow" id="txtDay'+count+'">Day - '+count+' : '+travel_date+'</div>'
+'<div class="table-responsive table-condensed">'
+'<table class="table borderless">'
+'<tbody>'
+'<tr>'
+'<td class="col-left">Travel time</td>'
+'<td class="col-middle"><input type="text" name="txtStartTime_detail[]" id="txtStartTime_detail"'+count+'" class="timepicker" value="'+start_time+'"/></td>'
+'<td class="col-middle"><input type="text" name="txtEndTime_detail[]" id="txtEndTime_detail'+count+'" class="timepicker" value="'+end_time+'" /></td>'
+'<td class="col-right"><div class="error" id="error_time'+count+'"> </div></td>'
+'</tr>'
+'<tr>'
+'<td class="col-left">Location</td>'
+'<td class="col-middle-2"><input type="text" size="100" name="txtStartLocation_detail[]" id="txtStartLocation_detail'+count+'" class="inputWithImge" value="'+start_location+'" onmouseover="display_text(this)" />'
+ '<img src="'+base_url+'" class="location-icon" alt="Click to search the location" name="location-icon" value="StartLocation_detail'+count+'" title="Click to show map"/>'
+ '</td>'
+'<td class="col-middle-2"><input type="text" name="txtEndLocation_detail[]" id="txtEndLocation_detail'+count+'" class="inputWithImge" value="'+end_location+'" onmouseover="display_text(this)"/>'
+ '<img src="'+base_url+'" class="location-icon" alt="Click to search the location" name="location-icon" value="EndLocation_detail'+count+'" title="Click to show map"/>'
+ '</td>'
+'<td class="col-right"><div class="error" id="error_location'+count+'"> </div></td>'
+'</tr>'
+'</tbody>'
+'<input type="hidden" name="txtStartLocation_Coordinates_detail[]" id="txtStartLocation_Coordinates_detail'+count+'" value="'+start_location_coor+'">'
+'<input type="hidden" name="txtEndLocation_Coordinates_detail[]" id="txtEndLocation_Coordinates_detail'+count+'" value="'+end_location_coor+'">'
+'</table>'
+'</div>';
travel_date = new Date(jQuery("#txtStartDate").val());
travel_date.setDate(startDate.getDate() + i);
travel_date = jQuery.datepicker.formatDate("DD, MM d, yy", new Date(travel_date));
}
html元素在codeigniter中动态填充,并以html字符串形式返回给浏览器。
这是我如何显示html字符串:
<form name="frm_RRequest" action="#" method="post">
<?php
if(strlen($current_request)>0 || $current_request!=null){
echo $current_request;
echo '<div><input type="button" name="btn_Update" class="button" value="Update" /></div>';
}else{
$site_url = site_url("user/recommendation_request");
echo '<div class="yellow">you have no current request, you can make a new request here >> <a href="'.$site_url.'">Recommendation Request</a></div>';
}
?>
</form>
以下是在codeigniter中创建元素的方法
'<div class="yellow" id="txtDay'.$count.'">Day - '.$count.' : '.$travel_date.'</div>'
.'<div class="table-responsive table-condensed">'
.'<table class="table borderless">'
.'<tbody>'
.'<tr>'
.'<td class="col-left">Travel time</td>'
.'<td class="col-middle"><input type="text" name="txtStartTime_detail[]" id="txtStartTime_detail"'.$count.'" class="timepicker" value="'.$start_time.'"/></td>'
.'<td class="col-middle"><input type="text" name="txtEndTime_detail[]" id="txtEndTime_detail'.$count.'" class="timepicker" value="'.$end_time.'" /></td>'
.'<td class="col-right"><div class="error" id="error_time'.$count.'"> </div></td>'
.'</tr>'
.'<tr>'
.'<td class="col-left">Location</td>'
.'<td class="col-middle-2"><input type="text" size="100" name="txtStartLocation_detail[]" id="txtStartLocation_detail'.$count.'" class="inputWithImge" value="'.$row->start_location.'" onmouseover="display_text(this)" />'
. '<img src="'.$base_url.'" class="location-icon" alt="Click to search the location" name="location-icon" value="StartLocation_detail'.$count.'" title="Click to show map"/>'
. '</td>'
.'<td class="col-middle-2"><input type="text" name="txtEndLocation_detail[]" id="txtEndLocation_detail'.$count.'" class="inputWithImge" value="'.$row->end_location.'" onmouseover="display_text(this)"/>'
. '<img src="'.$base_url.'" class="location-icon" alt="Click to search the location" name="location-icon" value="EndLocation_detail'.$count.'" title="Click to show map"/>'
. '</td>'
.'<td class="col-right"><div class="error" id="error_location'.$count.'"> </div></td>'
.'</tr>'
.'</tbody>'
.'<input type="hidden" name="txtStartLocation_Coordinates_detail[]" id="txtStartLocation_Coordinates_detail'.$count.'" value="'.$start_location_coor.'">'
.'<input type="hidden" name="txtEndLocation_Coordinates_detail[]" id="txtEndLocation_Coordinates_detail'.$count.'" value="'.$end_location_coor.'">'
.'</table>'
.'</div>';
答案 0 :(得分:1)
您的问题出在用于创建内容的字符串中。
你有
... id="txtStartTime_detail"'+count+'".....
将创建ID
...id = "txtStartTime_detail"1"...
您只需删除额外的双引号"'+count"'
应为'+count+'"