添加新的动态Boostrap卡不起作用

时间:2018-12-08 21:57:48

标签: javascript php jquery bootstrap-4

我将在页面内插入新的boostrap卡。我的问题是,我没有成功在内容中添加新卡。 当我单击插入时,卡不显示

代码下方

如果有记录,则显示新的boostrap卡

                  <div class="row">
                    <button type="button" class="btn btn-primary" aria-label="Insert"  id="insert">
                      <span aria-hidden="true">Insert</span>
                    </button>
                  </div>
                  <div class="row">
                    <ul class="row list-unstyled" id="list">
<?php
  $i = 0;

  while ($QoptionValue->fetch()) {
.....
?>
                      <li class="col-md-4" id="element'<?php echo $i; ?>">
                        <div class="card">
                          <h4 class="card-header">Card title <a class="close" href="#">×</a></h4>
                          <div class="card-body">
                            <div class="card-text">
                              <div><?php ..... ?></div>
                          </div>
                        </div>
                      </li>
<?php
    $i++;
  }
?>
                    </ul>
  </div>

在内容中显示新卡或第一张卡

<?php
$card = '<li class="col-md-4" id="element0">';
$card .= '<div class="card">';
$card .= '<h4 class="card-header">Card title <a class="close" href="#">Remove</a></h4>';
$card .= '<div class="card-body">';
$card .= '<div class="card-text">';
$card .= '<div>';

for ($l=0, $n=count($languages); $l<$n; $l++) {
  $card .= Language->getImage($languages[$l]['code']) . ' ' . HTML::inputField('option_value[' . $i . '][option_value_description][name][' . $l . ']', $options_name) . '<br />';
  $card .=  HTML::hiddenField('option_value[' . $i . '][option_value_description][language_id][' . $l . ']', $options_name);
}

$card .= '</div>';
$card .= '<div>';

$card .= HTML::inputField('option_value[' . $i . '][sort_order]', $QoptionValue->value('sort_order'), 'id="sort_order[' . $i . ']"');
$card .= '</div>';
$card .= '<div>';
$card .= '</div>';
$card .= '</div>';
$card .= '</li>';
?>

                <script>
                  $('#insert').click(function(){
                    $( "ul#list" ).append( "<?php echo $card; ?>" );
                  });
                </script>

                <script type="text/javascript">
                  $('.close').click(function(){
                    var $target = $(this).parents('li');
                    $target.hide('slow', function(){ $target.remove(); });
                  })
                </script>

控制台错误

这行似乎有问题:

                $( "ul#list" ).append(""<li class=\"col-md-4\" id=\"element0\"><div class=\"row\"><div class=\"col-md-12\"><div class=\"card\"><h4 class=\"card-header\"><a class=\"close\" href=\"#\">Supprimer<\/a><\/h4><div class=\"card-body\">Nom de la valeur<div><img src=\"http:\/\/localhost\/test_option\/shop\/sources\/third_party\/flag-icon-css\/flags\/4x3\/gb.svg\" alt=\"Anglais\" title=\"Anglais\" width=\"16\" height=\"12\" \/> <input type=\"text\" name=\"option_value[0][option_value_description][name][0]\" class=\"form-control\" \/><br \/><input type=\"hidden\" name=\"option_value[0][option_value_description][language_id][0]\" \/><img src=\"http:\/\/localhost\/test_option\/shop\/sources\/third_party\/flag-icon-css\/flags\/4x3\/fr.svg\" alt=\"Francais\" title=\"Francais\" width=\"16\" height=\"12\" \/> <input type=\"text\" name=\"option_value[0][option_value_description][name][1]\" class=\"form-control\" \/><br \/><input type=\"hidden\" name=\"option_value[0][option_value_description][language_id][1]\" \/><\/div><div class=\"row\"><span class=\"col-md-4\">Ordre de tri<\/span><\/div><\/div><\/div><\/div><\/div><\/li>"");


Uncaught SyntaxError: missing ) after argument list

1 个答案:

答案 0 :(得分:1)

优良作法是使用json_encode()将任何PHP变量传递给javascript。使用json_encode(),您将始终获得格式正确的JavaScript对象,并且转义了引号。

<script>
    $('#insert').click(function(){
        $( "ul#list" ).append(<?php echo json_encode($card); ?>);
    });
</script>