Reputation: 800
I have a text box for codeigniter shopping cart's qty.
<input type="text" id="txt-box_one<?php echo $items['id']; ?><?php echo $items['rowid'] ?>" value="<?php echo $items['qty']; ?> " class="small-txtbox">
I wrote a ajax function onmouseleave
for the textbox div.
if the user enters text box value as 4
at first and change it to 0
at second means i want to change the textbox value to 4
again.
js code:
function chng(id,rowid,divid,pid,price,divtotal)
{
var inc = $('#'+id).val();
var upd = 'update';
if(inc < 1)
{
// $('#'+id).val('1');
var retVal = confirm("Are You Sure to Remove This Item From Baskets?");
if( retVal == true ){
var inc = 0;
}
else { { $('#'+id).val(1); var inc = 1; } }
}
$.ajax({
type: "POST",
data: "qty="+inc+"&row="+rowid+"&upd="+upd+"&pid="+pid+"&price="+price,
url: "<?php echo base_url('grocery/text')?>",
success: function(html){
$('#'+divid).html(html);
if( retVal == true ){
location.reload();
}
}
});
}
Upvotes: 1
Views: 2188
Reputation: 234
You can do this using jQuery with something like :
jquery code:
<script type="text/javascript">
$(document).ready(function(){
$("#qty").change(function(){
if($(this).val()<=0){
$(this).val($("#qtyold").val());
}
var ch_val=$(this).val();
$("#qtyold").val(ch_val);
});
});
</script>
HTML Code:
<input type="text" id="qty" value="" />
Please let me know if it's working
Upvotes: 0
Reputation: 5308
function getValue() {
var val = $.trim($("#quantity").val());
val = parseFloat(val);
val = isNaN(val) ? 0 : val;
return val;
}
$("#quantity")
.on("keyup", function() {
var val = getValue();
if (val) {
$("#quantity").data("old-value", val)
}
})
.on("blur", function(){
var val = getValue();
if (val == 0) {
$("#quantity").val($("#quantity").data("old-value"));
}
})
Upvotes: 1
Reputation: 1427
You can try this jQuery approach also
var count;
$("#count").on("keyup",function(){
if($(this).val() == 0)
return;
count = $(this).val();
});
$("#count").on("blur",function(){
if($(this).val() == 0){
$(this).val(count);
}
});
Upvotes: 1
Reputation: 665
In order to compare in this way,
if(inc < 1)
you will have to parse it into an integer, so use
if(parseInt(inc) < 1)
Upvotes: 0