Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - Selecting default value in html dropdown list

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

<p>Price Band:<select id='priceBand' style = 'width:150px' value = 'band1'>
<option value="band7">Reduce by 30c</option>
<option value="band6">Reduce by 25c</option>
<option value="band5">Reduce by 20c</option>
<option value="band4">Reduce by 15c</option>
<option value="band3">Reduce by 10c</option>
<option value="band2">Reduce by 5c</option>
<option value="band1"  selected="selected">default</option>
</select></p>

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() {}

         });    


          } } ] });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")); 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...