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

时间:2022-01-08 08:53:31

I have the following which i use in a traditional onclick event for a DIV tag

我有以下内容,我在传统的onclick事件中使用DIV标签

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() {
    ????
  });
});

3 个解决方案

#1


$(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. You just need to construct the class attribute correctly in PHP to include the necessary data.

您也可以使用jquery.data()而不是使用子元素。您只需要在PHP中正确构造类属性以包含必要的数据。

#2


You could do something like:

你可以这样做:

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.

这是未经测试的,但它应该给你一个粗略的开始。

#3


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.

希望有所帮助。

#1


$(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. You just need to construct the class attribute correctly in PHP to include the necessary data.

您也可以使用jquery.data()而不是使用子元素。您只需要在PHP中正确构造类属性以包含必要的数据。

#2


You could do something like:

你可以这样做:

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.

这是未经测试的,但它应该给你一个粗略的开始。

#3


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.

希望有所帮助。