2015-05-03 54 views
0

我需要你的幫助來添加一個JavaScript變量內的HTML,PHP代碼我試過單引號和轉義'使用/裏面的var,但它不工作。HTML和PHP裏面的JavaScript變量

<script type='text/javascript'> 
 
var field = 
 
'<div class="form-group"> 
 
<label for="charge"><?php echo $lang['charge-formlabel']; ?></label> 
 
<input type="text" id="charge" name="charge[]" placeholder="<?php echo $lang['placeholder-addcharge']; ?>" /> 
 
<p class="help-block"> 
 
<?php echo $lang['charge-form-description']; ?> 
 
</p> 
 
</div> 
 
<div class="form-group"> 
 
<label for="description"><?php echo $lang['description-formlabel']; ?></label> 
 
<input type="text" id="description" name="description[]" placeholder="<?php echo $lang['placeholder-adddescription']; ?>" /> 
 
<p class="help-block"> 
 
<?php echo $lang['description-form-description']; ?> 
 
</p> 
 
</div> 
 
<div class="form-group"> 
 
<label for="value"><?php echo $lang['value-formlabel']; ?></label> 
 
<input type="text" id="value" name="value[]" placeholder="<?php echo $lang['placeholder-addvalue']; ?>" /> 
 
<p class="help-block"> 
 
<?php echo $lang['value-form-description']; ?> 
 
</p> 
 
</div>'; 
 
</script>

+0

不能在JavaScript中使用多行字符串,使用字符串連接 – vasilenicusor

+0

你應該考慮增加一些基本的縮進,使你的代碼更易讀。 –

回答

1

要在JavaScript字符串,你必須使用反斜槓轉義引號。其結果是,你的PHP代碼看起來就像這樣,一旦scaped:

$lang[\'charge-formlabel\']; 

請,也考慮考慮,如果你想使用JavaScript編寫一個多行字符串,你需要在最後加一個反彈每一行也是如此。

var field = '<div class="form-group">\ 
<label for="charge"><?php echo $lang[\'charge-formlabel\']; ?></label>'; 
+0

在多行字符串的行尾使用\字符的好處 – vasilenicusor

0

我修復了您的代碼。

<script type='text/javascript'> 
    var field = 
    '<div class="form-group">'+ 
    '<label for="charge"><?php echo $lang['charge-formlabel']; ?></label>' + 
    '<input type="text" id="charge" name="charge[]" placeholder="<?php echo $lang['placeholder-addcharge']; ?>" />' + 
    '<p class="help-block"><?php echo $lang['charge-form-description']; ?></p>' + 
    '</div>'+ 
    '<div class="form-group">'+ 
    '<label for="description"><?php echo $lang['description-formlabel']; ?></label>'+ 
    '<input type="text" id="description" name="description[]" placeholder="<?php echo $lang['placeholder-adddescription']; ?>" />'+ 
    '<p class="help-block"><?php echo $lang['description-form-description']; ?></p>' + 
    '</div>' + 
    '<div class="form-group">' + 
    '<label for="value"><?php echo $lang['value-formlabel']; ?></label>' + 
    '<input type="text" id="value" name="value[]" placeholder="<?php echo $lang['placeholder-addvalue']; ?>" />' + 
    '<p class="help-block"><?php echo $lang['value-form-description']; ?></p>'+ 
    '</div>'; 
</script> 

請參閱Google JavaScript Style Guide

不要這樣做:

var myString = 'A rather long string of English text, an error message \ 
       actually that just keeps going and going -- an error \ 
       message to make the Energizer bunny blush (right through \ 
       those Schwarzenegger shades)! Where was I? Oh yes, \ 
       you\'ve got an error and all the extraneous whitespace is \ 
       just gravy. Have a nice day.'; 

在每行開頭的空格不能在編譯時安全地剝離;斜槓後的空格將導致棘手的錯誤;儘管大多數腳本引擎都支持這一點,但它不是ECMAScript的一部分。

使用字符串連接來代替:

var myString = 'A rather long string of English text, an error message ' + 
    'actually that just keeps going and going -- an error ' + 
    'message to make the Energizer bunny blush (right through ' + 
    'those Schwarzenegger shades)! Where was I? Oh yes, ' + 
    'you\'ve got an error and all the extraneous whitespace is ' + 
    'just gravy. Have a nice day.';