gpt4 book ai didi

javascript - jQuery 现有 html 元素的未定义值

转载 作者:行者123 更新时间:2023-12-03 09:01:17 25 4
gpt4 key购买 nike

我有一个 jQuery 代码来检查具有特定 id 的 html 元素是否存在。我已经编写了现有和不存在的 html 元素的 if 和 else 子句。但是当元素存在时,为什么 jQuery 返回的值是未定义的?

您可以看到下面的 jQuery 代码。变量 start_time 已定义,因为它进入了子句条件,但该值被检测为未定义。代码有什么问题?

  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+'">&nbsp;</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+'">&nbsp;</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.'">&nbsp;</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.'">&nbsp;</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>';

最佳答案

您的问题在于您用来创建内容的字符串。

你有

... id="txtStartTime_detail"'+count+'".....

这将创建 ID

...id = "txtStartTime_detail"1"...

您只需删除多余的双引号 "'+count"' 应该是 '+count+'"

关于javascript - jQuery 现有 html 元素的未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32299489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com