如何在jquery选择器中传递php变量值?

时间:2012-12-17 13:06:41

标签: php jquery mysql jquery-selectors

我想突出显示从mysql数据库中检索id的映射图像的那部分。 我已将id存储在$ loc变量中。现在使用jquery选择器我想在我的jquery选择器中传递这个php变量,这样它将突出显示与$ loc中具有相同id的图像部分。 这是我的工作代码:

    <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.maphilight.js"></script>
<script type="text/javascript">
 $(function () {
        $(".mapping").maphilight();


    $(".hoverable").css("border", "3px solid red");


    var data = $('#cr2').data('maphilight') || {};  
    data.alwaysOn = !data.alwaysOn;
    $('#cr1').data('maphilight', data).trigger('alwaysOn.maphilight'); // portion to always highlight (I WANT TO PASS THE PHP VARIABLE $LOC INSTEAD OF '#CR1'

});<area shape="poly" id="cr1" class="room" coords="549,272,489,334,534,377,594,316" href="www.msn.com" target="_top" alt="CR1" name="room-1">

2 个答案:

答案 0 :(得分:4)

#cr1 替换为#<?php echo $loc ?>

而不是:

$('#cr1').data('maphilight', data).trigger('alwaysOn.maphilight');

使用:

$('#<?php echo $loc ?>').data('maphilight', data).trigger('alwaysOn.maphilight');

答案 1 :(得分:-1)

您可以使用json_encode函数并将php变量$loc作为参数传递

例如

var data = <?php echo json_encode($loc); ?>;

jQuery.each(data, function(i,event) {
   // alert(event.toSource());
   var id = event.id;  // here id is the array of the value based on the php variable for $loc
   badge_name = event.name; // here name is the array of the value based on the php variable for $loc
});
相关问题