 |
ปัญหาสร้าง button บวก ลบ จำนวนสินค้าพร้อมอับเดดออโต้ |
|
 |
|
|
 |
 |
|
Code (PHP)
https://stackoverflow.com/questions/31103893/number-increment-button
Code (PHP)
https://css-tricks.com/number-increment-buttons/
|
 |
 |
 |
 |
Date :
2017-08-18 14:52:25 |
By :
Pong Thep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<input type='text' name='qty1' id='qty1' value='<?php echo $objResult["Packing"];?>' readonly = 'readonly'/>
<input type='text' name='qty' id='qty' value='<?php echo $objResult["Packing"];?>' readonly = 'readonly'/>
<input type='button' name='add' value='+'/>
<input type='button' name='subtract' value='-'/>
<script type="text/javascript">
with( document.getElementById( 'frmCart' ) )
incrementButtons( add, subtract, qty );
</script>
Code (Java)
<script type="text/javascript">
function incrementButtons( upBtn, downBtn, qtyField )
{
var step = parseFloat( qtyField.value ) || 1,
currentValue = step;
downBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue -= Math.min( step, currentValue - step ) );
}
upBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue += step );
}
}
</script>
มันได้ดังรู้ค่ะ

เวลากดบวกมันบวกแค่แถวแรกค่ะ แถวต่อไปไม่เพิ่มให้
และไม่อับเดดให้ค่ะ กดรีเฟส ค่าก็หาย
ช่วยด้วยค้า
|
ประวัติการแก้ไข 2017-08-19 15:57:23
 |
 |
 |
 |
Date :
2017-08-19 15:56:55 |
By :
nud |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<button type="button" class="txtQua-left-minus btn btn-danger btn-number"
data-type="minus" data-package="<?=$packing?>"><span class="glyphicon glyphicon-minus"></span></button>
<input type="text" id="txtQua<?=$strNum;?>" name="txtQua[]" class="span1 form-control input-number" placeholder="1" min="1" max="100" value="<?=$_SESSION["strQuanlity"]["$i"];?>" maxlength="6" readonly>
<button type="button" class="txtQua-right-plus btn btn-success btn-number"
data-type="plus" data-package="<?=$packing?>"><span class="glyphicon glyphicon-plus"></span></button>
<script>
$(document).ready(function(){
var quantitiy=0;
$('.txtQua-right-plus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
var txtQua = parseInt($('#txtQua').val());
// If is not undefined
$('#txtQua').val(txtQua + parseInt($(this).data('package')) );
// Increment
});
$('.txtQua-left-minus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
var txtQua = parseInt($('#txtQua').val());
// If is not undefined
// decrement
if(txtQua>0){
$('#txtQua').val(txtQua - $(this).data('package') );
}
});
});
</script>
|
 |
 |
 |
 |
Date :
2017-08-19 18:11:28 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
[php]<input type='text' name='qty' id='qty' value="<?php echo $objResult["Packing"];?>" readonly = 'readonly'/>
<input type='button' name='add' value='+'/>
<input type='button' name='subtract' value='-'/>
[/php
Code (Java)
<script type="text/javascript">
with( document.getElementById( 'frmCart' ) )
incrementButtons( add, subtract, qty );
</script>
Code (Java)
<script type="text/javascript">
function incrementButtons( upBtn, downBtn, qtyField )
{
var step = parseFloat( qtyField.value ) || 1,
currentValue = step;
downBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue -= Math.min( step, currentValue - step ) );
}
upBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue += step );
}
}
</script>

เวลากดบวกมันบวกแค่แถวแรกค่ะ แถวต่อไปไม่เพิ่มให้
และไม่อับเดดให้ค่ะ กดรีเฟส ค่าก็หาย
ช่วยด้วยค้า
|
ประวัติการแก้ไข 2017-08-21 09:39:33
 |
 |
 |
 |
Date :
2017-08-21 09:39:03 |
By :
nud |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ส่วนใหญ่ผมจะตอบเป็นแนวคิด อันนี้พอดีว่างเลยทำเป็นตัวอย่างให้
ตัวอย่าง ทดสอบ
Code (PHP)
<?php
$testarray=array(
(object)array('id'=>1, 'name'=>'stockname1', 'packing'=>6),
(object)array('id'=>2, 'name'=>'stockname2', 'packing'=>12),
(object)array('id'=>3, 'name'=>'stockname3', 'packing'=>24),
(object)array('id'=>4, 'name'=>'stockname4', 'packing'=>144),
);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Button inc-dec</title>
<link rel="stylesheet" type="text/css" href="asset/css/bootstrap.css">
<script src="asset/js/jquery-1.11.3.min.js"></script>
<script src="asset/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('.qty-minus').click(function(){
var pkg = parseInt($(this).data('package'));
var qty = $(this).parent().find('.span1').eq(0);
var val = parseInt($(qty).val());
if(val<pkg){
$(qty).val(0);
}else{
$(qty).val(val - pkg);
}
});
$('.qty-plus').click(function(){
var pkg = parseInt($(this).data('package'));
var qty = $(this).parent().find('.span1').eq(0);
var val = parseInt($(qty).val());
$(qty).val(val + pkg);
});
});
</script>
</head>
<body>
<div class="container">
<form>
<?php foreach($testarray as $row):?>
<div class="row">
<label><?=$row->name?></label>
<button type="button" class="qty-minus btn btn-danger" data-package="<?=$row->packing?>"><span class="glyphicon glyphicon-minus"></span></button>
<input type="text" name="txtQua[<?=$row->id?>]" class="span1 input-number" value="0" readonly>
<button type="button" class="qty-plus btn btn-success" data-package="<?=$row->packing?>"><span class="glyphicon glyphicon-plus"></span></button>
</div>
<?php endforeach;?>
</form>
</div>
</body>
</html>
|
 |
 |
 |
 |
Date :
2017-08-21 10:54:02 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่พี่ที่มาตอบนะค่ะ ขอบคุณมาก
|
ประวัติการแก้ไข 2017-08-22 09:05:35 2017-08-22 14:23:01
 |
 |
 |
 |
Date :
2017-08-22 08:59:51 |
By :
nud |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|