无法.append();没有消除代码中的所有空格

时间:2010-12-30 04:42:14

标签: php javascript jquery

$('#form_holder').append('<div id="spec_id_'+count+'"><div class="avail_container">

<input class="avail_fields" type="checkbox" checked="checked" name="special'+count+'" /><span class="avail_field_label">Special Date</span></div>
<div class="avail_container"><div class="avail_time_container"><span class="field_label">Time</span>
    <select name="special'+count+'_time_from_1">

    <?php
        for ($t = 0; $t<24; $t++){
        ?>
        <option value="<?php echo $t; ?>"><?php echo $t; ?></option>

        <?php
        }

        ?>
    </select>:
    <select name="special'+count+'_time_from_2">
            <?php
        for ($t = 0; $t<60; $t+=15){
        ?>
        <option value="<?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?>"><?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?></option>

        <?php
        }

        ?>
    </select>

    <span class="field_label">to</span>
    <select name="special'+count+'_time_to_1">
        <?php
        for ($t = 0; $t<24; $t++){
        ?>
        <option value="<?php echo $t; ?>"><?php echo $t; ?></option>

        <?php
        }

        ?>
    </select>:
    <select name="special'+count+'_time_to_2">
                    <?php
        for ($t = 0; $t<60; $t+=15){
        ?>
        <option value="<?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?>"><?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?></option>

        <?php
        }

        ?>
    </select>

    </div>
</div>
</div>');

我假设javascript或jquery不喜欢我在这里的休息,因为我的所有javascript代码都不起作用。

什么是消除所有空间的替​​代方案,这会使查看代码变得困难?

3 个答案:

答案 0 :(得分:0)

你可以逃脱换行符。

var str = 'this is a string\
           on multiple lines\
           with the line breaks escaped.'

或连接字符串。

var str = 'this is a string' +
          'on multiple lines' +
          'with the line breaks escaped.'

答案 1 :(得分:0)

好的,首先,允许javascript字符串跨越多行,每行都有一个反斜杠

等...

var string = 'This is a javascript\
              string that spans\
              multiple lines';

其次,PHP代码无法在javascript中呈现,因此您的代码将被简单地显示,而不会像您希望的那样进行处理。

我建议使用ajax出去并抓住你想要追加的html,以便处理php。

答案 2 :(得分:0)

尝试不检查

var varVal1=<?php
        for ($t = 0; $t<24; $t++){
        ?>
            <option value="<?php echo $t; ?>"><?php echo $t; ?></option>
        <?php
        }
        ?>;
var varVal2=<?php
        for ($t = 0; $t<60; $t+=15){
        ?>
            <option value="<?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?>"><?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?></option>
        <?php
        }
        ?>
var varVal3=<?php
        for ($t = 0; $t<24; $t++){
        ?>
            <option value="<?php echo $t; ?>"><?php echo $t; ?></option>
        <?php
        }
        ?>
var varVal4=<?php
        for ($t = 0; $t<60; $t+=15){
        ?>
            <option value="<?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?>"><?php if($t == 0){ echo $t . '' . $t; }else{ echo $t; } ?></option>
        <?php
        }
        ?>
$('#form_holder').append('<div id="spec_id_'+count+'">'+
    '<div class="avail_container">'+
    '<input class="avail_fields" type="checkbox" checked="checked" name="special'+count+'" />'+
    '<span class="avail_field_label">Special Date</span>'+
    '</div>'+
    '<div class="avail_container">'+
    '<div class="avail_time_container">'+
    '<span class="field_label">Time</span>'+
    '<select name="special'+count+'_time_from_1">'+varVal1+
    '</select>:'+
    '<select name="special'+count+'_time_from_2">'+varVal2+
    '</select>'+
    '<span class="field_label">to</span>'+
    '<select name="special'+count+'_time_to_1">'+varVal3+
    '</select>:'+
    '<select name="special'+count+'_time_to_2">'+varVal4+             
    '</select>'+
    '</div>'+
'</div>'+
'</div>');

关于

<强> Wazzy