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

标签 javascript jquery html

我有一个 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/

相关文章:

javascript - 如何从 javascript 变量中隐藏最后一个表的列?

html - 子元素的 z-index 是父元素的本地元素吗?

JavaScript - 将一种方法应用于多个对象

javascript - Momentjs isDST 函数不返回正确的结果

javascript - 经过一段时间间隔后滚动到 anchor

javascript - Jquery - 使用一个动态值多次重用函数

html - css:将文本包装到 div 中,因此 div 占用文本的长度

javascript - 如何使用 jQuery 找到表格中最右下角的单元格?

javascript - 如何从 Javascript 准备/构建多维数组以发送到 PHP

javascript - 使用 AJAX 通过 Ruby on Rails 验证优惠券后获取数据