简体   繁体   English

如何将其绑定到JQUERY中的onclick事件?

[英]How would I bind this to the onclick event in JQUERY?

I have the following which i use in a traditional onclick event for a DIV tag 我在DIV标签的传统onclick事件中使用了以下内容

How would I go about implementing this using jquery? 我将如何使用jquery来实现呢?

$parameter = "onClick=\"selprice('" .
  $price_options_array[$price_counter]['price'] . "','" .
  $price_counter . "')" . "\"";

I use the above and just add it to my div like so: 我使用上面的方法,只是将其添加到我的div中,如下所示:

<div class="price_row" <?php echo $parameter; ?>>

I have this so far using the jquery method but Im not sure how to proceed? 到目前为止,我已经使用了jquery方法,但是我不确定如何进行?

$(document).ready(function() {
  $('.price_row').click(function() {
    ????
  });
});
$(function() {
  $("div.price_row").click(function() {
    selprice("<?php echo $price_options_array[$price_counter]['price'] ?>", "<?php echo $price_counter ?>");
  });
});

I wouldn't necessarily advise it however. 但是,我不一定会建议。 Another approach is to put the necessary date in a place you can query it at runtime. 另一种方法是将必要的日期放在可以在运行时查询的地方。 For example: 例如:

<div class="price_info")
  <div class="price"><?php echo $price_options_array[$price_counter]['price'] ?></div>
  <div class="counter"><?php echo $price_counter ?></div>
</div>

combined with CSS: 结合CSS:

div.price_info div.price, div.price_info div.counter { display: none; }

and then: 接着:

$(function() {
  $("div.price_row").click(function() {
    var price = $(this).children("price").text();
    var counter = $(this).children("counter").text();
    selprice(price, counter);
  });
});

Dynamically generated Javascript can sometimes get a bit messy. 动态生成的Javascript有时会有些混乱。

You can also use jquery.data() instead of using child elements. 您也可以使用jquery.data()代替子元素。 You just need to construct the class attribute correctly in PHP to include the necessary data. 您只需要在PHP中正确构造class属性即可包含必要的数据。

Move your $price_options_array to a javascript array. 将$ price_options_array移到javascript数组。

'var price_options = ' . $price_options  // or however you do it in PHP

And when you make your div inside your loop add an extra tag with the price_counter (pc) 当您将div放入循环中时,请添加一个额外的标签,其中包含price_counter(pc)

"<div pc='". $price_counter . "'class="price_row" <?php echo $parameter; ?>>"

The "pc" variable will then be accessible in the jquery loop, as will the price_options info 然后,可以在jquery循环中访问“ pc”变量,以及price_options信息

$(document).ready(function() {

  $('.price_row').click(function() {
     var pc = $(this).attr('pc');
     var price = price_options[pc]['price'];
     selprice(price, pc);
  });
});

Hope that helps. 希望能有所帮助。

You could do something like: 您可以执行以下操作:

PHP: PHP:

<script language="text/javascript">var price_options = <?php echo json_encode($price_options_array); ?></script

for ( $y = 0; $y < $prices; $y++ )
{
  echo '<div class="price_row" id="price_' . $y . '">Foo</div>';
}

And in jQuery: 在jQuery中:

$('div.price_row').click(function(){
    var id = $(this).attr('id').split('_');
    selprice(price_options[id], id);
});

This is untested but it should give you a rough start. 这未经测试,但是应该可以让您有个大概的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM