在Jquery脚本中,如下所示。细节的元素是在jquery函数内动态创建的。但是,将变量“startLocation”中的值分配给输入“detail_startLocation []”时,该值未完全分配。这似乎只是第一个字。
例如,“startLocation”的值是“Grand Hyatt”,但只有“Grand”显示为“detail_StartLocation []”的值。在警告变量时,它返回完整值。有没有人知道什么是错的?
var startLocation = jQuery("#txtStartLocation").val();
var endLocation = jQuery("#txtEndLocation").val();
var date2 = jQuery.datepicker.formatDate("DD, MM d, yy", new Date(jQuery("#txtStartDate").val()));
for(i=1; i<= n_day;i++){
var detail = '<div> </div>'
+'<div><h4>Day-'+i+' : '+date2+'</h4></div>'
+'<div class="table">'
+'<div class="tr">'
+'<div class="td"> </div>'
+'<div class="td">Start</div>'
+'<div class="td">End</div>'
+'</div>'
+'<div class="tr">'
+'<div class="td">Travel time</div>'
+'<div class="td"><input type="text" name="detail_startTime[]" value= '+startTime+' /></div>'
+'<div class="td"><input type="text" name="detail_endTime[]" value= '+endTime+' /></div>'
+'</div>'
+'<div class="tr">'
+'<div class="td">Location</div>'
+'<div class="td"><input type="text" size="100" name="detail_startLocation[]" value= '+startLocation+' /></div>'
+'<div class="td"><input type="text" name="detail_endLocation[]" value= '+endLocation+' /></div>'
+'</div>'
+'</div>';
jQuery("#detail").append(detail);
date2 = new Date(jQuery("#txtStartDate").val());
date2.setDate(startDate.getDate() + i);
date2 = jQuery.datepicker.formatDate("DD, MM d, yy", new Date(date2));
}
答案 0 :(得分:1)
您正在插入没有引号("
)的值。在HTML中,每个不包含纯字符的值都必须用引号括起来(例如value="Grand Hyatt"
而不是value=Grand Hyatt
)。
仅添加引号可能不够。出于多种原因,您应该正确地转义值。由于您已经在使用jQuery,我建议使用库的.val
函数将值插入DOM。你可以这样做:
// Wrap HTML code in jQuery instance for later modifications
var detail = $('<div> </div>'
+'<div><h4>Day-'+i+' : '+date2+'</h4></div>'
+'<div class="table">'
+'<div class="tr">'
+'<div class="td"> </div>'
+'<div class="td">Start</div>'
+'<div class="td">End</div>'
+'</div>'
+'<div class="tr">'
+'<div class="td">Travel time</div>'
// Don't set the value but add a class attribute
+'<div class="td"><input type="text" name="detail_startTime[]" class="start-time" value="" /></div>'
// Don't set the value but add a class attribute
+'<div class="td"><input type="text" name="detail_endTime[]" class="end-time" value="" /></div>'
+'</div>'
+'<div class="tr">'
+'<div class="td">Location</div>'
// Don't set the value but add a class attribute
+'<div class="td"><input type="text" size="100" name="detail_startLocation[]" class="start-location" value="" /></div>'
// Don't set the value but add a class attribute
+'<div class="td"><input type="text" name="detail_endLocation[]" class="end-location" value="" /></div>'
+'</div>'
+'</div>');
// Update value attributes
detail.find('input.start-time').val(startTime);
detail.find('input.end-time').val(endTime);
detail.find('input.start-location').val(startLocation);
detail.find('input.end-location').val(endLocation);
// Append to "real" DOM
jQuery("#detail").append(detail);
首先,我们在输入元素中添加一些CSS类,以便稍后选择它们。整个HTML代码包装在一个jQuery实例中,然后我们可以相应地进行修改。
答案 1 :(得分:0)
如果您查看生成的HTML,则会注意到value
不在引号中。
例如,这段代码:
value= '+startLocation+'
真的应该是这样的:
value="'+startLocation+'"