在JavaScript函数中使用选择器“ $(this).find()” jQuery

吉安卡洛斯

我正在使用jQuery,这是我用于任何标签的click事件的代码。

$('.spin span:last-child').click(function(){
  unitPrice = parseFloat($(this).closest('.product').find('.unit-price span').text().substring(3));
  if ( $(this).parent().find($('.custom-input')).val() == "" ) {
    $(this).parent().find($('.custom-input')).val(1);
    inputValue = parseInt($(this).parent().find($('.custom-input')).val());
    subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
    $(this).closest('.product').find('.total-price span').text(subTotal);
  } else if ( inputValue =! "" ) {
    inputValue = parseInt($(this).parent().find($('.custom-input')).val());
    inputValue += 1
    $(this).parent().find($('.custom-input')).val(inputValue);
    subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
    $(this).closest('.product').find('.total-price span').text(subTotal);
  };
});

因此,我创建了一个函数来优化代码:

      function getValues(){
        unitPrice = parseFloat($(this).closest('.product').find('.unit-price span').text().substring(3));
        subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
        return subTotal;
        $(this).closest('.product').find('.total-price span').text(subTotal);
      };

我的新块代码应如下所示:

$('.spin span:last-child').click(function(){
  if ( $(this).parent().find($('.custom-input')).val() == "" ) {
    $(this).parent().find($('.custom-input')).val(1);
    inputValue = parseInt($(this).parent().find($('.custom-input')).val());
    getValues();
  } else if ( inputValue =! "" ) {
    inputValue = parseInt($(this).parent().find($('.custom-input')).val());
    inputValue += 1
    $(this).parent().find($('.custom-input')).val(inputValue);
    getValues();
  };
});

但是我的函数“ getValues”不起作用,函数内部的选择器“ $(this).find ...”应该是我认为的问题,你们可以帮忙解决此问题吗?

谢谢

老的

我建议不要更改函数定义,以允许将您正在使用的元素传递给它,而不用担心在this内部getValues进行构造。

您的函数中还存在一些错误:return如果该函数执行,则ing将结束执行,因此后一行return当前将不运行,您应该始终使用var来声明变量。您没有使用此函数的结果,因此您实际上根本不需要该return语句。

function getValues(spanEl) {
    var unitPrice = parseFloat($(spanEl).closest('.product').find('.unit-price span').text().substring(3));
    var subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
    $(spanEl).closest('.product').find('.total-price span').text(subTotal);
  };

然后随便打电话getValues()getValues(this);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

找出函数中使用的jQuery选择器

来自分类Dev

在jQuery选择器中使用javascript参数

来自分类Dev

在 Jquery 中使用选择器

来自分类Dev

如何在jQuery日期和时间选择器中使用javascript / jQuery调整时区?

来自分类Dev

跳过jQuery.find()中的选择器递归?

来自分类Dev

jQuery上下文选择器与.find()

来自分类Dev

jQuery链中的选择器后如何使用多个.find()?

来自分类Dev

jQuery属性选择器使用Find的区别

来自分类Dev

使用jQuery(或javascript)从CSS选择器中选择文本

来自分类Dev

在Jquery Ajax中使用选择器和$(this)

来自分类Dev

jQuery选择器“包含”在chrome中使用时出错

来自分类Dev

在Internet Explorer中使用多个选择器进行jquery

来自分类Dev

如何在JQuery中使用动态选择器

来自分类Dev

在jQuery中使用日期选择器,今天显示

来自分类Dev

在jQuery选择器中使用Codeigniter代码

来自分类Dev

在Internet Explorer中使用多个选择器进行jquery

来自分类Dev

如何在on方法jQuery中使用多个选择器?

来自分类Dev

在 jQuery 中使用数据属性对象作为选择器

来自分类Dev

如何在表选择器中使用 jQuery 变量

来自分类Dev

如何在 jQuery 中使用 .not 选择器?

来自分类Dev

在选择器名称中使用 jQuery 变量

来自分类Dev

Javascript或jQuery选择器从`a`到使用类的很多div

来自分类Dev

使用 javascript queryselectorall 代替 jquery 选择器

来自分类Dev

如何访问用jQuery中的.find()选择的选择器的内部html?

来自分类Dev

jQuery find()等价于javascript

来自分类Dev

jQuery find()等价于javascript

来自分类Dev

jQuery选择器无法在脚本中使用,但可以在控制台中使用

来自分类Dev

何时使用jQuery的.find()

来自分类Dev

时间选择器验证?jQuery / JavaScript

Related 相关文章

  1. 1

    找出函数中使用的jQuery选择器

  2. 2

    在jQuery选择器中使用javascript参数

  3. 3

    在 Jquery 中使用选择器

  4. 4

    如何在jQuery日期和时间选择器中使用javascript / jQuery调整时区?

  5. 5

    跳过jQuery.find()中的选择器递归?

  6. 6

    jQuery上下文选择器与.find()

  7. 7

    jQuery链中的选择器后如何使用多个.find()?

  8. 8

    jQuery属性选择器使用Find的区别

  9. 9

    使用jQuery(或javascript)从CSS选择器中选择文本

  10. 10

    在Jquery Ajax中使用选择器和$(this)

  11. 11

    jQuery选择器“包含”在chrome中使用时出错

  12. 12

    在Internet Explorer中使用多个选择器进行jquery

  13. 13

    如何在JQuery中使用动态选择器

  14. 14

    在jQuery中使用日期选择器,今天显示

  15. 15

    在jQuery选择器中使用Codeigniter代码

  16. 16

    在Internet Explorer中使用多个选择器进行jquery

  17. 17

    如何在on方法jQuery中使用多个选择器?

  18. 18

    在 jQuery 中使用数据属性对象作为选择器

  19. 19

    如何在表选择器中使用 jQuery 变量

  20. 20

    如何在 jQuery 中使用 .not 选择器?

  21. 21

    在选择器名称中使用 jQuery 变量

  22. 22

    Javascript或jQuery选择器从`a`到使用类的很多div

  23. 23

    使用 javascript queryselectorall 代替 jquery 选择器

  24. 24

    如何访问用jQuery中的.find()选择的选择器的内部html?

  25. 25

    jQuery find()等价于javascript

  26. 26

    jQuery find()等价于javascript

  27. 27

    jQuery选择器无法在脚本中使用,但可以在控制台中使用

  28. 28

    何时使用jQuery的.find()

  29. 29

    时间选择器验证?jQuery / JavaScript

热门标签

归档