Javascript值在选择列表中更改

时间:2016-01-11 05:22:05

标签: javascript php

我在列表框上设计并开发了一个关于更改的JavaScript代码..

下面是图片: enter image description here

此处,点击“运输下拉列表”后,应更改总金额... 它在第一行工作,但在第二行JS没有工作..

表格代码:

<tr>
    <td>1</td>
    <td>Multiple Products</td>
    <td>60</td>
    <td>
        <select name="shippingCompany" id="shippingCompany" class="form-control input-sm mb10" required onchange="getprice();">
            <option selected='selected' value=''>- - - - - - - - -</option>
            <option value='1~190'>DHL (190)</option>
            <option value='4~143'>Aromax (143)</option>
            <option value='5~224'>BlueDart   (224)</option>
        </select>
    </td>
    <td>300                     </td>
    <td>500                         <input type="hidden" name="consolidateFees" id="consolidateFees" value="500">
        <input type="hidden" name="storageFees" id="storageFees" value="300">
        <input type="hidden" name="totalWeight" id="totalWeight" value="60">
        <input type="hidden" name="userCountry" id="userCountry" value="230">
        <input type="hidden" name="orderNo" id="orderNo" value="1">
        <input type="hidden" name="userId" id="userId" value="7">
        <input type="hidden" name="totalAmtPaid" id="totalAmtPaid" value="">
        <input type="hidden" name="shippingCompanySelected" id="shippingCompanySelected" value="">
        <input type="hidden" name="shippingAmt" id="shippingAmt" value="">
    </td>
    <td><span id="totalAmt"></span></td>
    <td><button type="submit" name="PayNowBtn" id="PayNowBtn" class="btn btn-info">Pay Now</a></td>
<tr>

上表代码在PHP中,而循环

JS CODE:

function getprice()
    {
        alert("assaas");
        var totalWght = document.getElementById("totalWeight").value;
        var consolidateFees = document.getElementById("consolidateFees").value;
        var storageFees = document.getElementById("storageFees").value;
        var shippingCompany = document.getElementById("shippingCompany").value;     
        var shipSplit = shippingCompany.split("~"); 
        var userCountry = document.getElementById("userCountry").value;

        $.ajax({
            url:"ajax.php",
            type:"post",
            data:{"type":"calculatePrice",totalW:totalWght, cFess:consolidateFees, sFees:storageFees, shipComp:shipSplit[0], uCountry:userCountry },
            success:function(data){
                document.getElementById("totalAmt").innerHTML = data;
                document.getElementById("totalAmtPaid").value = data;
                document.getElementById("shippingCompanySelected").value = shipSplit[0];
                document.getElementById("shippingAmt").value = shipSplit[1];
            }                         
        });  
    }

2 个答案:

答案 0 :(得分:0)

document.getElementById("totalAmt").innerHTML = data;

表示您始终替换元素“totalAmt”的值。 因此,为每个元素使用单独的Id。

答案 1 :(得分:0)

谢谢朋友们,

我在创建PHP While循环时使用了动态ID ..并且代码工作正常..

我已将该ID传递给每个输入ID和JS ID