jquery没有';t从DIV数据id中获取PHP变量值,只输出变量名


jquery doesn't get PHP variable value from DIV data-id, just outputs variable name

我有:

<div class= "obutton feature2" data-id=".$bookID."><button>Reserve Book</button></div> 
<div class="modal-box"></div>

这是一个调用jquery的按钮,但它生成的模式框只输出".$bookID.",而不是其中的实际值。

jquery脚本是:

<script>
    $('button').click(function() 
    {
        var book_id = $(this).parent().data('id'),
        result = "Book #" + book_id + " has been reserved.";
        $('.modal-box').text(result).fadeIn(700, function() 
        {
            setTimeout(function() 
            {
        $('.modal-box').fadeOut();
            }, 2000);
        });
    });
</script>

感谢所有帮助-Tom

如果是php,那么它应该在标签中:

<div class= "obutton feature2" data-id="<?php echo $bookID; ?>">
  <button>Reserve Book</button>
</div> 

或者,如果整件事是一个打印字符串,那么你必须转义你的引号或使用单引号:

<?php
  echo '<div class= "obutton feature2" data-id="' . $bookID . '">
     <button>Reserve Book</button>
  </div>'; 
?>