Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   В чем ошибка (https://javascript.ru/forum/misc/67545-v-chem-oshibka.html)

рони 24.02.2017 12:24

MixPetrov,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <style type="text/css">
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
  <script>
$ (function	() {
  function mathC(){
    var nmb = +this.dataset.nmb;
    var ns = +this.value;
    ns = ns || 0;
    ns = Math.ceil(ns/nmb)*nmb;
    this.value = ns
    }
    var timer;
    $('.quantity_cart [type="text"]').on("input", function() {
        window.clearTimeout(timer);
        timer = window.setTimeout(mathC.bind(this), 1200)
    })
});

  </script>
</head>

<body class="quantity_cart">
<input name="1"   size="2" value="1" type="text" data-nmb="3">
<input name="1"   size="2" value="1" type="text" data-nmb="3">
</body>
</html>

MixPetrov 24.02.2017 14:29

Не работает
А можно ли к функции подключить таймер без
$('.quantity_cart [type="text"]').on("input", function()


$ (function	() {
  function mathC(){
    var nmb = +($('input[name=\'' + product_id + '\']').val().dataset.nmb);
   // var ns = +this.value;
    //ns = ns || 0;
    //ns = Math.ceil(ns/nmb)*nmb;
    //this.value = ns
		var ns = +$('input[name=\'' + product_id + '\']').val();
		ns = ns || 0;
		ns = Math.ceil(ns/nmb)*nmb;
		$('input[name=\'' + product_id + '\']').val(ns)
	}
    var timer;
    $('.quantity_cart [type="text"]').on("input", function() {
        window.clearTimeout(timer);
        timer = window.setTimeout(mathC.bind(this), 1200)
    })
});

рони 24.02.2017 14:34

MixPetrov,
пас

MixPetrov 24.02.2017 15:17

Подскажите в чем делаю ошибку?
$ (function	() {
  function mathC(){
    var nmb = +($('input[name=\'' + product_id + '\']').val().dataset.nmb);
   // var ns = +this.value;
    //ns = ns || 0;
    //ns = Math.ceil(ns/nmb)*nmb;
    //this.value = ns
		var ns = +$('input[name=\'' + product_id + '\']').val();
		ns = ns || 0;
		ns = Math.ceil(ns/nmb)*nmb;
		$('input[name=\'' + product_id + '\']').val(ns)
	}
    var timer;
    $('.quantity_cart [type="text"]').on("input", function() {
        window.clearTimeout(timer);
        timer = window.setTimeout(mathC.bind(this), 1200)
    })
});

рони 24.02.2017 15:36

MixPetrov,
вы пишите бред, переменная product_id с неба прилетит.
Цитата:

Сообщение от MixPetrov
Не работает

если здесь работает то и у вас будет работать

MixPetrov 24.02.2017 15:44

Согласен с выше написанным.

В файле js
$ (function	() {
  function mathC(product_id, nmb){
    var nmb = +($('input[name=\'' + product_id + '\']').val().dataset.nmb);
   // var ns = +this.value;
    //ns = ns || 0;
    //ns = Math.ceil(ns/nmb)*nmb;
    //this.value = ns
		var ns = +$('input[name=\'' + product_id + '\']').val();
		ns = ns || 0;
		ns = Math.ceil(ns/nmb)*nmb;
		$('input[name=\'' + product_id + '\']').val(ns)
	}
    var timer;
    $('.quantity_cart [type="text"]').on("input", function() {
        window.clearTimeout(timer);
        timer = window.setTimeout(mathC, 1200)
    })
});


В файле php (firebug)

<div class="quantity_cart">
<span class="minus" onclick="quantity(2, '-10', '20', '125')"><</span>
<input name="2" oninput="mathC(2,10); isright(2,125,20); recalc(2);" size="2" value="20" maximum="125" quantity="125" minimum="10" data-nmb="10" type="text">
<span class="plus" onclick="quantity(2, '+10', '10', '125')">></span>
</div>

рони 24.02.2017 15:47

MixPetrov,
не будет работать

MixPetrov 24.02.2017 15:50

Куда копать?

рони 24.02.2017 16:10

MixPetrov,
https://learn.javascript.ru/events-and-interfaces
https://learn.javascript.ru/event-details
https://learn.javascript.ru/attribut...tom-properties

MixPetrov 24.02.2017 16:21

Спасибо


Часовой пояс GMT +3, время: 10:47.