jQuery 旋转类型的东西

发布于 2024-08-17 16:10:42 字数 897 浏览 3 评论 0原文

今天早些时候,我试图创建一个 jQuery spinner 类型的东西,有人给了我这段代码,它可以在单击按钮时向上/向下增加文本字段值。极好的。但是,如果该值为 0 - 零,您该怎么做才能禁用 .desc 按钮。在 PHP 中非常容易 if if <=0 then this 等等...但我不知道 jQuery..

还有什么想法如何使用它来向上/向下移动无序的 html 列表即 ul li?

$(document).ready(function() 
{   
    $(function()
    {
        $(".inc").click(function() 
        { 
            $(":text[name='qty']").val(Number($(":text[name='qty']").val()) + 1);
        }); 

        $(".dec").click(function()
        {      
            $(":text[name='qty']").val(Number($(":text[name='qty']").val()) - 1);
        });  
    });
});

这使用了一种形式:

<input type="text" name="qty" value="0" />
<img src="img/up.png" class="inc" width="20px" height="9px" alt="Increase" title="increase" />
<img src="img/down.png" class="dec" width="20px" height="9px" alt="Decrease" title="Decrease" />

I'm trying to create a jQuery spinner type thing earlier today somebody gave me this code which increases the text field value up/down on button clicks. Fantastic. But what do you do to disable the .desc button if the value is 0 - zero. In PHP very easy if if <=0 then this etc... but I don't know jQuery..

Also any ideas how it can be used to move up/down an unordered html list i.e. ul li?

$(document).ready(function() 
{   
    $(function()
    {
        $(".inc").click(function() 
        { 
            $(":text[name='qty']").val(Number($(":text[name='qty']").val()) + 1);
        }); 

        $(".dec").click(function()
        {      
            $(":text[name='qty']").val(Number($(":text[name='qty']").val()) - 1);
        });  
    });
});

This uses a form:

<input type="text" name="qty" value="0" />
<img src="img/up.png" class="inc" width="20px" height="9px" alt="Increase" title="increase" />
<img src="img/down.png" class="dec" width="20px" height="9px" alt="Decrease" title="Decrease" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心在旅行 2024-08-24 16:10:42

这是我的代码,

$(document).ready(function () {
  var textElem = $(":text[name='qty']"),
      getTextVal = function() {
        var val = parseInt(textElem.val(), 10);
        return isNaN(val) ? 0 : val;
      };

  $(".inc").click(function () {
      textElem.val(getTextVal() + 1);
  });

  $(".dec").click(function(){
      if( getTextVal() == 0) {
         return false;
      }

      textElem.val(getTextVal() - 1);
  });
});

当文本元素的值达到 0 时,图像“.dec”停止递减,您可以动态添加和删除图像中的类名,以便用户可以注意到转换。

建议使用按钮来实现此目的,并且可以使用 css 对其进行样式设置以根据需要显示。

这不是优化的代码,但应该让您了解如何使其按照您的要求工作。

here is my code,

$(document).ready(function () {
  var textElem = $(":text[name='qty']"),
      getTextVal = function() {
        var val = parseInt(textElem.val(), 10);
        return isNaN(val) ? 0 : val;
      };

  $(".inc").click(function () {
      textElem.val(getTextVal() + 1);
  });

  $(".dec").click(function(){
      if( getTextVal() == 0) {
         return false;
      }

      textElem.val(getTextVal() - 1);
  });
});

The image '.dec' stops decrementing as the value of the text element reaches 0, you can dynamically add and remove the class name(s) to the image so that the user can notice the transition.

It is suggested to use buttons for this purpose and they can be styled with css to appear as required.

This is not the optimized code, but should give you an idea of how to get it working as per ur requirements.

泪是无色的血 2024-08-24 16:10:42

嗯,jQuery 仍然是 javascript,所以你为什么不使用

$(".dec")
    .click(
        function(){
            if(Number($(":text[name='qty']").val() > 0)
            {
                $(":text[name='qty']")
                    .val( Number($(":text[name='qty']").val()) - 1 
                );
            }
         }
     );

我的意思是,javascript 有 if's。
如果我理解正确的话

uhm jQuery is still javascript, so why don't you use

$(".dec")
    .click(
        function(){
            if(Number($(":text[name='qty']").val() > 0)
            {
                $(":text[name='qty']")
                    .val( Number($(":text[name='qty']").val()) - 1 
                );
            }
         }
     );

I mean, javascript has if's.
If i understood you correctly

终陌 2024-08-24 16:10:42

由于代码使用图像而不是按钮或输入元素,因此您无法真正禁用它。最简单的方法是隐藏它:

$(":text[name='qty']").change(function() {
    if($(this).val() <= 0) {
        $(".dec").hide();
    } else {
        $(".dec").show();
    }
}):

如果将减量“按钮”更改为输入或按钮元素,则可以将功能代码更改为:

    if($(this).val() <= 0) {
        $(".dec").attr('disabled','disabled');
    } else {
        $(".dec").removeAttr('disabled');
    }

Since the code uses an image instead of a button or input element, you can't truly disable it. The easiest thing to do would be to hide it which would be:

$(":text[name='qty']").change(function() {
    if($(this).val() <= 0) {
        $(".dec").hide();
    } else {
        $(".dec").show();
    }
}):

If you change the decrement "button" to an input or button element, you could change the function code to:

    if($(this).val() <= 0) {
        $(".dec").attr('disabled','disabled');
    } else {
        $(".dec").removeAttr('disabled');
    }
青春如此纠结 2024-08-24 16:10:42

使用单独的函数来处理微调器的增量和减量。禁用仅适用于表单元素,但由于您使用图像表示微调器控件,因此您可以对这些元素调用 .hide().show() ,或者跳过对 incrementdecrement 调用中的操作。

function increment() {
    var textField = $(":text[name='qty']");
    var value = Number(textField.val()) + 1;
    textField.val(value);
}

function decrement() {
    var textField = $(":text[name='qty']");
    var value = Number(textField.val()) - 1;
    textField.val(value);
    if(value == 0) {
        $(".dec").attr("disable", "disable");
    }
}

$(document).ready(function() {
    $(".inc").click(increment);
    $(".dec").click(decrement);
});

您可以使用此微调器在无序列表中向上或向下移动,但您必须跟踪列表中的当前位置。 decrement 函数已经注意不要低于 0。如果您位于列表中的最后一项,则还需要禁用增量。像这样的东西应该可以工作:

if(current == $("#list > li").size() - 1) {
    $(".inc").attr("disable", "disable");
}

当然,当值再次更改时必须重新启用这些。但我确信你能做到。

Using separate functions to handle the increment and decrement from a spinner. The disabling will only work on form elements, but since you are representing the spinner controls using images, you could call .hide() and .show() on those, or skip the action in the calls to increment and decrement.

function increment() {
    var textField = $(":text[name='qty']");
    var value = Number(textField.val()) + 1;
    textField.val(value);
}

function decrement() {
    var textField = $(":text[name='qty']");
    var value = Number(textField.val()) - 1;
    textField.val(value);
    if(value == 0) {
        $(".dec").attr("disable", "disable");
    }
}

$(document).ready(function() {
    $(".inc").click(increment);
    $(".dec").click(decrement);
});

You can use this spinner to go up or down an unordered list, but you will have to keep track of the current position in the list. The decrement function already takes care of not going below 0. The increment needs to be disabled as well if you're at the last item in the list. Something like this should work:

if(current == $("#list > li").size() - 1) {
    $(".inc").attr("disable", "disable");
}

Of course, these have to be re-enabled when the value changes again. But I'm sure you can do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文