html下拉框设置默认值,在html下拉列表中选择默认值

I am currently looking to have a drop down list in my form. I have this drop down which selects the default value:

Price Band:

Reduce by 30c

Reduce by 25c

Reduce by 20c

Reduce by 15c

Reduce by 10c

Reduce by 5c

default

Which works fine and selects the default as the default value. But what I also need to be able to do - after the form is submitted I want it to keep the last selected value as the default one. It is a form used to add sales with different price bands. The sales are entered by the price bands so the defaults are entered first, the band2, band3 and so on.. What is the best way of doing it? I am currently using javascript and php on the page if that makes it any easier?

Ajax code. I didn't include getting the value of the dropdown as this is only a new thing that I am implementing. I just want to know if it is possible to have a default value selected when the form is loaded first and then when a different value is selected, to keep that value as the new default:

$('#divItemisedSaleAdd').dialog({'autoOpen': false, 'modal' : true, 'buttons' :

[ { text: "Ok", click: function() {

var url = '<?php echo Navigation::gUrl('/users/admin/stocktake_details_sales.php', array('stocktake_id' => $stocktake_id, 'action' => 'add_itemised_sale'));?>';

var productCode = $('#ProductCode').val();

var qty = $('#Quantity').val();

var dialog = this;

$.ajax({

url: url,

dataType: 'json',

data: {'productCode' : productCode, 'qty' : qty},

type: 'post',

timeout: 5000,

success: function(json) {

if (json.status == 'S'){

alert('Sale added');

}

else if (json.status == 'E')

alert('No product with given PLU was found! Please check!');

// loadDepartments();

$( dialog ).dialog( "close" );

},

error: function() {}

});

} } ] });

解决方案

You can use localStorage for that purpose:

$('#divItemisedSaleAdd').dialog({'autoOpen': false, 'modal' : true, 'buttons' :

[ { text: "Ok", click: function() {

var url = '<?php echo Navigation::gUrl('/users/admin/stocktake_details_sales.php', array('stocktake_id' => $stocktake_id, 'action' => 'add_itemised_sale'));?>';

var productCode = $('#ProductCode').val(),

qty = $('#Quantity').val(),

dialog = this;

// save current selected value in storage

localStorage.setItem("default_option", productCode);

$.ajax({

url: url,

dataType: 'json',

data: {'productCode' : productCode, 'qty' : qty},

type: 'post',

timeout: 5000,

success: function(json) {

if (json.status == 'S'){

alert('Sale added');

}

else if (json.status == 'E')

alert('No product with given PLU was found! Please check!');

// loadDepartments();

$( dialog ).dialog( "close" );

},

error: function() {}

});

} } ] });

// after page reload

if (localStorage.getItem("default_option")) {

$('#ProductCode').val(localStorage.getItem("default_option"));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值