美元符号 jquery 作为字符串:未终止的字符串文字



纵了一些 html 以在模态引导程序中显示一个小表单,如下所示:

$.each(response, function(i, item){
    detail += "<div><input name='detail-tipe' type='checkbox' value='" + response[i].nama_detail + "' class='detail-tipe' /> " + response[i].nama_detail.toString() + "</div>";
});
detail +=  "$('.detail-tipe').each(function(){
    var attrib = $(this).attr('value');
    for ( var j=0, k = remo.length; j < k; j++ ) {
        if(attrib == remo[j]){
            $(this).attr('checked', 'checked');
        };
    }
})";

但是,美元符号是未终止的。可能吗请指教。

如果要将多行字符串分配给变量,则需要在每行末尾放置一个,以告诉解释器字符串正在继续。否则,它会认为您要开始一行新的实际代码。

detail += "$('.detail-tipe').each(function(){ 
  var attrib = $(this).attr('value'); 
  for ( var j=0, k = remo.length; j < k; j++ ) { 
    if(attrib == remo[j]){ 
        $(this).attr('checked', 'checked'); 
    }; 
  } 
})";

最新更新