在html下拉列表中选择默认值

时间:2016-01-15 13:24:46

标签: javascript php html

我目前正在寻找表单中的下拉列表。我有这个下拉列表,它选择默认值:

<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>

哪种方法正常,并选择默认值作为默认值。但我还需要做的是 - 在提交表单后,我希望它将最后选择的值保留为默认值。它是一个用于添加不同价格区间销售的表单。销售是通过价格区域输入的,因此首先输入默认值,band2,band3等等。最好的方法是什么?我目前在页面上使用javascript和php,如果这更容易吗?

Ajax代码。我没有包含获取下拉列表的价值,因为这只是我正在实施的新事物。我只是想知道在首先加载表单时是否可以选择默认值,然后选择不同的值时,将该值保持为新的默认值:

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

         });    


          } } ] });

2 个答案:

答案 0 :(得分:1)

您可以将localStorage用于此目的:

$('#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")); 
}

答案 1 :(得分:0)

这可能会这样做 - selected = "selected"selected,具体取决于您是否使用XHTML。它需要使值成为一个简单的整数,但这样可以简化清理它,只需使用您在查询中使用的intval()以及控制所选选项。这个版本假设页面提交,现在显然不是这样,因为它全部由Ajax完成,但希望它对某人有用。

    if(!isset($priceBand_value)) $priceBand_value = array();
    $priceBand_value = ''; // reset it if already used
    // set default 'selected = "selected"'; or just 'selected'; 
    <?php if(!isset($_POST['priceBand']) $priceBand_value[1] = 'selected = "selected"'; ?>
    <?php $priceBand_value[intval($_POST['priceBand'])] = 'selected = "selected"'; ?>

    <p>Price Band:<select id='priceBand' style = 'width:150px'>
    <option value="7" <?php echo $priceBand_value[7] ?>>Reduce by 30c</option>
    <option value="6" <?php echo $priceBand_value[6] ?>>Reduce by 25c</option>
    <option value="5" <?php echo $priceBand_value[5] ?>>Reduce by 20c</option>
    <option value="4" <?php echo $priceBand_value[4] ?>>Reduce by 15c</option>
    <option value="3" <?php echo $priceBand_value[3] ?>>Reduce by 10c</option>
    <option value="2" <?php echo $priceBand_value[2] ?>>Reduce by 5c</option>
    <option value="1" <?php echo $priceBand_value[1] ?>>default</option>
    </select></p>

对于大多数情况,您可以从0而不是1开始,但这主要是为了适应给定的代码框架。这样可以避免设置默认值,因为intval($_POST['priceBand'])在首次渲染页面时会为0,但在这种情况下,可能更难跟踪&#34; band&#34;。