Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Как вывести результат из скрипта (https://javascript.ru/forum/misc/82444-kak-vyvesti-rezultat-iz-skripta.html)

sotex2 08.05.2021 08:40

Как вывести результат из скрипта
 
Подскажите, как вывести результат из скрипта ниже в форму (смотреть скрин)





$(document).ready(function(){
	function price(col){
		var price;
		if(col >= 500 && col <= 1000){
			price = 0.4;
		}
		if(col >= 1001 && col <= 5000){
			price = 0.3;
		}
		if(col >= 5001 && col <= 10000){
			price = 0.2;
		}
		if(col >= 10001 && col <= 50000){
			price = 0.1;
		}
		if(col >= 50001){
			price = 0.08;
		}
		return price;
	}

	$('input.one').on('input keyup', function(e){
		var col = $(this).val();
		var price = price(col);
		var total = price * col;
		$('input.two').val(total);
		$('input.three').val(price);
});
	
});

рони 08.05.2021 09:31

sotex2,
проявите фантазию в именах!!! строка 24, и сделайте значение по умолчанию строка 3, либо проверку перед умножением.

sotex2 08.05.2021 10:10

ссори, я новичок и был бы рад любой помощи.

рони 08.05.2021 10:18

sotex2,
<!DOCTYPE html>

<html>
<head>
    <title>Untitled</title>
    <meta charset="utf-8">
    <style type="text/css">
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
$(function(){
    function getPrice(col){
        var price = 1;
        if(col >= 500 && col <= 1000){
            price = 0.4;
        }
        if(col >= 1001 && col <= 5000){
            price = 0.3;
        }
        if(col >= 5001 && col <= 10000){
            price = 0.2;
        }
        if(col >= 10001 && col <= 50000){
            price = 0.1;
        }
        if(col >= 50001){
            price = 0.08;
        }
        return price;
    }

    $('input.one').on('input', function(e){
        var col = this.value;
        var price = getPrice(col);
        var total = price * col;
        $('input.two').val(total);
        $('input.three').val(price);
});

});

    </script>
</head>

<body>
<input type="number" class="one">
<input type="number" class="two">
<input type="number" class="three">

</body>
</html>

рони 08.05.2021 10:23

sotex2,
обновите страницу, пример выше.

sotex2 08.05.2021 11:11

Спасибо большое.
Не подскажите, как во втором поле <input type="number" class="two">
не выводились числа, что после запятой? скрин приложил.


рони 08.05.2021 13:57

sotex2,
строка 36
$('input.two').val(Math.trunc(total));


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