简体   繁体   English

现有html元素的jQuery未定义值

[英]jQuery undefined value of an existing html element

I have a jQuery code to check whether a html element with particular id exists or not. 我有一个jQuery代码来检查是否存在具有特定ID的html元素。 I have written the if and else clause of existing and non existing html element. 我已经编写了现有和不存在的html元素的if和else子句。 But when the element exists why jQuery returns the value as undefined ? 但是当元素存在时,为什么jQuery返回未定义的值?

You can see the jQuery code below. 您可以在下面看到jQuery代码。 The variable start_time is defined, because it enters the clause condition but the value is detected as undefined. 之所以定义了变量start_time,是因为它输入了子句条件,但是该值被检测为未定义。 What is wrong with the code? 代码有什么问题?

  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));


    }

the html elements are populated dynamically in codeigniter and returns to browsers as a html String. html元素会在codeigniter中动态填充,并以html字符串形式返回到浏览器。

here is how I display the html string : 这是我显示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> 

and here is how the elements are created in codeigniter 这是在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>';

Your problem is in the strings that you use to create the content. 您的问题出在用于创建内容的字符串中。

You have 你有

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

which will create the ID 这将创建ID

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

You just have to remove the extra double quotes "'+count"' should be '+count+'" 您只需要删除多余的双引号, "'+count"'应该为'+count+'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM