简体   繁体   English

基于class + data-attribute的按钮调度

[英]Button disabilitation based on class + data-attribute

I'm writing an <input type="number"> with - and + button for an ecommerce cart. 我正在用电子商务购物车的-+按钮写一个<input type="number">

The structure of each group is: 每组的结构是:

  • 1x <button class="minus" data-prod="prod_id_int"> 1x <button class="minus" data-prod="prod_id_int">
  • 1x <input type="number" id="prod_id_int"> 1x <input type="number" id="prod_id_int">
  • 1x <button class="plus" data-prod="prod_id_int"> 1x <button class="plus" data-prod="prod_id_int">

What I'm trying to do now is disabling the button - if the value of the input type number is < 1 . 我现在要做的是禁用按钮-如果输入类型编号的值< 1

To achieve it, based on my script, I have to disable not the general <button class="minus"> but the specific <button class="minus" data-prod="prod_id_int"> . 为了实现它,基于我的脚本,我必须禁用一般的<button class="minus">但特定的<button class="minus" data-prod="prod_id_int"> I tried this 我试过这个

$(buttonClass).data('prod', dataProd).prop('disabled', true);

and it actually prevents the quantity from being < 1 BUT it doesn't really add the property disabled to the button. 它实际上阻止了数量< 1但它并没有真正将属性disabled到按钮。 I'm not sure, then, that it's the right way. 那时我不确定这是正确的方法。 Can someone explain me how to achieve it? 有人能解释我如何实现它吗?

Here the working snippet 这里是工作片段

 $(document).ready(function() { $('button').on('click', function() { var buttonClass = $(this).attr('class'); //console.log(buttonClass); var buttonID = $(this).attr('id'); //console.log(buttonID); var dataProd = $(this).data('prod'); var inputToChange = $('#' + dataProd); var inputToChangeValue = $('#' + dataProd).val(); if (buttonClass == 'minus') { var newValue = parseInt(inputToChangeValue) - parseInt(1); if (newValue < 1) { $(buttonClass).data('prod', dataProd).prop('disabled', true); //$(buttonClass).data('prod="' + dataProd + '"').prop('disabled', true); //$(buttonClass + '.[data-prod="' + dataProd + '"]').attr(disabled=disabled); //.prop('disabled', true) //alert('NOPE'); } else { $('#' + dataProd).val(newValue); //console.log(inputToChangeValue); } } else if (buttonClass == 'plus') { var newValue = parseInt(inputToChangeValue) + parseInt(1); if (newValue > 99) { alert('NOPPPPEE'); } else { $('#' + dataProd).val(newValue); console.log(inputToChangeValue); } } }); }); 
 .plus, .minus { width: 1.5%; height: auto; background-color: #EF1B1F; border-radius: 50%; text-align: center; display: inline-block; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="minus" data-prod="23">-</button> <input type="number" class="qta" id="23" value="5"> <button class="plus" data-prod="23">+</button> <br> <button class="minus" data-prod="90">-</button> <input type="number" class="qta" id="90" value="62"> <button class="plus" data-prod="90">+</button> 

Here is solution. 这是解决方案。

$('.'+buttonClass).data('prod', dataProd).prop('disabled', true);

All you need is to concatenate . 所有你需要的是concatenate . symbol to your class. 你的班级的象征。

buttonClass returns only the className , such as minus and you need jquery selector , like this: $('.minus') buttonClass只返回className ,例如minus ,你需要jquery selector ,如下所示: $('.minus')

Also, I recommend you to use this: var newValue = --inputToChangeValue; 另外,我建议你使用它: var newValue = --inputToChangeValue; for a simply way to decrement value, instead var newValue = parseInt(inputToChangeValue) - parseInt(1); 一个简单的方法来减少值,而不是var newValue = parseInt(inputToChangeValue) - parseInt(1);

 $(document).ready(function() { $('button').on('click', function() { var buttonClass = $(this).attr('class'); //console.log(buttonClass); var buttonID = $(this).attr('id'); //console.log(buttonID); var dataProd = $(this).data('prod'); var inputToChange = $('#' + dataProd); var inputToChangeValue = $('#' + dataProd).val(); if (buttonClass == 'minus') { var newValue = --inputToChangeValue; if (newValue < 1) { $('.'+buttonClass).filter(function() { return $(this).data("prod") == dataProd }).prop('disabled', true); //$(buttonClass).data('prod="' + dataProd + '"').prop('disabled', true); //$(buttonClass + '.[data-prod="' + dataProd + '"]').attr(disabled=disabled); //.prop('disabled', true) //alert('NOPE'); } else { $('#' + dataProd).val(newValue); //console.log(inputToChangeValue); } } else if (buttonClass == 'plus') { var newValue = parseInt(inputToChangeValue) + parseInt(1); $('.minus').filter(function() { return $(this).data("prod") == dataProd }).prop('disabled', false); if (newValue > 99) { alert('NOPPPPEE'); } else { $('#' + dataProd).val(newValue); console.log(inputToChangeValue); } } }); }); 
 .plus, .minus { width: 10%; height: auto; background-color: #EF1B1F; border-radius: 50%; text-align: center; display: inline-block; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="minus" data-prod="23">-</button> <input type="number" class="qta" id="23" value="5"> <button class="plus" data-prod="23">+</button> <br> <button class="minus" data-prod="90">-</button> <input type="number" class="qta" id="90" value="12"> <button class="plus" data-prod="90">+</button> 

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

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