将变量传递给Jquery函数

时间:2017-10-21 14:28:40

标签: jquery

我正在尝试将按钮单击的内容传递给Jquery函数。

我的脚本正在使用" Stephan Wanger的JBox插件"这是创建弹出窗口的特殊功能..

我的按钮代码:

<div class="imagehelp" id="imaint_help">
  <div onClick='javascript:clickMeImage()'>
    <div class="detailButton" id="TrafficM1">
  <a href="#?Record=<?php echo $row_m['RecordID']; ?>"><img src="nav/trafficeDetail_button.png" width="91" height="59" /></a>
    </div>
  </div>
</div>

我的Jquery功能:

function clickMeImage() {
var record = "<?php echo $_GET['Record'];?>";
console.log(record);
new jBox('Modal', {
  attach: '.imagehelp',
  width: 1000,
  height: 500,
  title: 'Traffic detail',
  theme: 'TooltipBorder',
  closeButton: 'title',
  draggable: 'title',
  trigger: 'click',
  animation: 'false',
  position: {
    x: 'center',
    y: 'center',
  },
  offset: {x: 15, y: -10},
  onCloseComplete: function() {
    this.destroy();
    $('#jBox-overlay').remove();
  },
  ajax: {
    url: 'traffic_detail.php?RecordID=record',
    reload: 'strict'
  }
});
}

非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

您从父元素触发功能。我应该这样做:

<?php
$row_m['RecordID'] = "something";
?>
<div class="imagehelp" id="imaint_help">
    <div onClick='javascript:clickMeImage("<?php echo $row_m['RecordID']; ?>")'>
        <div class="detailButton" id="TrafficM1">
            <img src="nav/trafficeDetail_button.png" width="91" height="59"/>
        </div>
    </div>
</div>
<script>
    function clickMeImage(mydata) {
        alert(mydata);
    }
</script>

如果你想保留锚,你可以使用一个属性:

    <?php
$row_m['RecordID'] = "other-something";
?>
<div class="imagehelp" id="imaint_help">
    <div onClick='javascript:clickMeImage()'>
        <div class="detailButton" id="TrafficM1">
            <a href="#" data-record="<?php echo $row_m['RecordID']; ?>"> <img src="nav/trafficeDetail_button.png" width="91" height="59"/></a>
        </div>
    </div>
</div>
<script>
    function clickMeImage() {
        var el = document.querySelector('a');
        var mydata = el.getAttribute('data-record')
        alert(mydata);
    }
</script>