0

它一直在下面给我一个未终止的字符串文字错误,但我看不到问题。如何在下面修复它:

 var $fileImage = $("<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >
    <p class='f1_upload_process'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='f1_upload_form'><br/><label>
    File: <input name='fileImage' type='file' class='fileImage' /></label><label><input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>
    </p> <iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #fff;'></iframe></form>");

$image.append($fileImage);
4

1 回答 1

4

JavaScript 不是 PHP。字符串中不允许换行。

\在每行的末尾添加,或使用 . 连接字符串" + "

// Using \, scroll to the right to see how to use it
var $fileImage = $("<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >\
    <p class='f1_upload_process'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='f1_upload_form'><br/><label>\
    File: <input name='fileImage' type='file' class='fileImage' /></label><label><input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>\
    </p> <iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #fff;'></iframe></form>");

// Using string concatenation
var $fileImage = $("<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >" + 
    "<p class='f1_upload_process'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='f1_upload_form'><br/><label>" + 
    "File: <input name='fileImage' type='file' class='fileImage' /></label><label><input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #fff;'></iframe></form>");

由于您使用的是jQuery,我建议使用 jQuery 创建可读且可维护的代码:

var $form = $('<form>', {
    action: 'upload.php',
    method: 'post',
    enctype: 'multipart/form-data',
    target: 'upload_target'
}).submit(startUpload);
// Et cetera.
于 2012-04-07T20:01:56.980 回答