jQuery未定义现有html元素的值

jQuery undefined value of an existing html element

本文关键字:元素 html 未定义 jQuery      更新时间:2023-09-26

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

你可以看到下面的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元素在编码器中动态填充,并作为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+'"