0

好的,基本上我有一个表格,您可以在其中输入要购买的物品,在此期间您可以选择标准并显示折扣,这些折扣适用于您实时添加的物品,现在折扣可以正常使用它们是常规文本字段,但是将它们切换到下拉列表以限制可以输入的值破坏了我的代码,好奇是否有人可以帮助我...这两个表单元素的 ID 为 st_disc 和 sh_disc .. .

这是表单中的 HTML,忽略所有额外的 php,尽管如此,如果表单提交不正确,它会记住选择的选项...

    <td colspan="2" style="border-width:0px; padding:10px; text-indent: 10px;"><p class="tdblackbigger">Standard Discount:</p></td>
    <td style="border-width:0px; padding:10px; text-indent: -105px;"><p class="tdblackbigger">
        <?php $restdisc = $_REQUEST['st_discount']; ?>
        <?php $attr7 = 'selected="selected"'; ?>
        <select name="st_discount" id="st_disc" class="invoiceselect">
            <option selected="selected" value="">------</option>
            <option value="20"<?php echo $restdisc == '20' ? $attr7 : ''; ?>>20</option>
            <option value="35"<?php echo $restdisc == '35' ? $attr7 : ''; ?>>35</option>
            <option value="40"<?php echo $restdisc == '40' ? $attr7 : ''; ?>>40</option>
            <option value="45"<?php echo $restdisc == '45' ? $attr7 : ''; ?>>45</option>
        </select>
    </td>
    <td colspan="2" style="border-width:0px; padding:10px;"><p class="tdblackbigger">Show Discount:</p></td>
    <td style="border-width:0px; padding:10px; text-indent: -135px;"><p class="tdblackbigger">
        <?php $reshdisc = $_REQUEST['sh_discount']; ?>
        <?php $attr8 = 'selected="selected"'; ?>
        <select name="sh_discount" id="sh_disc" class="invoiceselect">
            <option selected="selected" value="">------</option>
            <option value="5"<?php echo $reshdisc == '5' ? $attr8 : ''; ?>>5</option>
            <option value="10"<?php echo $reshdisc == '10' ? $attr8 : ''; ?>>10</option>
            <option value="15"<?php echo $reshdisc == '15' ? $attr8 : ''; ?>>15</option>
            <option value="20"<?php echo $reshdisc == '20' ? $attr8 : ''; ?>>20</option>
            <option value="25"<?php echo $reshdisc == '25' ? $attr8 : ''; ?>>25</option>
        </select>
    </td>

这是在它们是文本框时编写的 JS,我比 JS 更擅长 PHP,所以它可能很简单..

$(document).ready(function() {   
    $("input").keyup(function() {
    var subtotal = 0;
    var stantot = 0;
    var showtot = 0;
        for (var i = 0; i <= 30; i++) {
        var unitp = parseFloat($("#unitp" + i).val()) || 0;
        var casep = parseFloat($("#casep" + i).val()) || 0;
        var units = parseFloat($("#units" + i).val()) || 0;
        var cases = parseFloat($("#cases" + i).val()) || 0;
        var st_disc = parseFloat($("#st_disc").val()) || 0;
        var sh_disc = parseFloat($("#sh_disc").val()) || 0;

        var unitr = (unitp * units);
        var caser = (casep * cases);
        var result = (unitr + caser);
        var st_disc_fix = (st_disc / 100);
        var sh_disc_fix = (sh_disc / 100);
        var st_disc_solo = (st_disc_fix * result);
        var sh_disc_solo = (sh_disc_fix * result);
        var st_disc_amt = (result - st_disc_solo);
        var sh_disc_amt = (st_disc_amt - sh_disc_solo);
        var disc_total = (st_disc_fix + sh_disc_fix);
        var disc_whole = (disc_total * result);

        var disc = (result - disc_whole);
        var st_disc_tot = (result - disc_whole);
        var sh_disc_tot = (result - disc_whole);

        $("#line" + i).val(result.toFixed(2));
        $("#disc" + i).val(disc.toFixed(2));
        subtotal += parseFloat(result);
        stantot += parseFloat(st_disc_amt);
        showtot += parseFloat(sh_disc_amt);
    }
    $("#totretail").val(subtotal.toFixed(2));
    $("#standiscount").val(stantot.toFixed(2));
    $("#showdiscount").val(showtot.toFixed(2));

    var totship = ($("#totship").val() * 1);

    var finaltotal = (showtot + totship);
    $("#total").val(finaltotal.toFixed(2));

    });
 });
4

1 回答 1

0

看起来你错过了这里的重点..

var st_disc = parseFloat($("#st_disc").val());
var sh_disc = parseFloat($("#sh_disc").val());

$("#st_disc").val()   and  $("#sh_disc").val()  is messing your logic here

“#st_disc”是所选元素的id,它没有值..里面的选项将有一个选定的值..

所以 var st_disc = parseFloat($("#st_disc").val()); 应该是

     var st_disc = parseFloat($("#st_disc  option:selected").val()) || 0;

var sh_disc = parseFloat($("#sh_disc option:selected").val()) || 0;


                            OR

var st_disc = parseFloat($("#st_disc").find(":selected").val()) || 0;

var sh_disc = parseFloat($("#sh_disc").find(":selected").val()) || 0;​ 
于 2012-09-14T20:49:17.930 回答