jQuery表单添加不适用于下拉列表

jQuery form addition not working with drop downs

本文关键字:适用于 下拉列表 不适用 添加 表单 jQuery      更新时间:2023-09-26

好吧,基本上我有一个表单,你可以在其中输入你想购买的商品,在这个表单中的任何时候,你都可以选择一个标准并显示折扣,这些折扣适用于你实时添加的商品,现在折扣都可以正常使用,它们都是常规文本字段,但将它们切换到下拉列表以限制可以输入的值已经破坏了我的代码,好奇是否有人能帮我。。。两个表单元素的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));
    });
 });

看起来您错过了要点。。

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