当我想为另一个表单

时间:2017-04-24 20:20:59

标签: javascript dom

最近,我已经问了一个关于确认的问题并且它已经解决了,但现在的问题是代码仅用于id,当我尝试为类创建它只更改第一个表单而不是一秒钟。



function fuelPrice()
{
    var fuelPrice=0;
    var theForm = document.forms["price"];
    var withFuelPrice = document.getElementsByClassName("ful");
    if(withFuelPrice[0].checked==true)
    {
        fuelPrice=30;
    }
    return fuelPrice;
}

function withPol()
{
    var polishPrice=0;
    var theForm = document.forms["price"];
    var includeInscription = document.getElementsByClassName("pol");
    if(includeInscription[0].checked==true){
        polishPrice=50;
    }
    return polishPrice;
}

function driver()
{
    var driverPrice=0;
    var theForm = document.forms["price"];
    var getDriv = document.getElementsByClassName("drv");
    if(getDriv[0].checked==true){
        driverPrice=50;
    }
    return driverPrice;
}

function calculateTotal()
{
var car1= 50
    var total = fuelPrice() + withPol() + car1 + driver();
     var text = "Total Price For the Renting "+total+ "BHD/Week";
    //display the result
    var divobj = document.getElementsByClassName('totalPrice');
    divobj[0].style.display='block';
    divobj[0].innerHTML = text;
  
    return text;

}

function myFunction()
{
	var name = calculateTotal()
	confirm(name)
}

				<form class="price"><p class="totalPrice booktxt">Total Price For the Renting 50BHD/Week<br> </p>
                <input onclick="calculateTotal() " type="checkbox" class="ful">With Fuel<br>
                <input onclick="calculateTotal() " type="checkbox" class="pol">Polishing 2 weeks<br>
                <input onclick="calculateTotal() " type="checkbox" class="drv">Driver<br>

                </form>
            <button class="btn1" onclick="myFunction()">Add to cart</button>



<form class="price"><p class="totalPrice booktxt">Total Price For the Renting 50BHD/Week<br> </p>
                <input onclick="calculateTotal() " type="checkbox" class="ful">With Fuel<br>
                <input onclick="calculateTotal() " type="checkbox" class="pol">Polishing 2 weeks<br>
                <input onclick="calculateTotal() " type="checkbox" class="drv">Driver<br>

                </form>
            <button class="btn1" onclick="myFunction()">Add to cart</button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

看看我是否理解......

您应该分开表格。检查此代码......

&#13;
&#13;
undefined
&#13;
function fuelPrice(form)
{
    var fuelPrice=0;
    var theForm = document.forms["price"];
    var withFuelPrice = document.getElementsByClassName("ful");
    if(withFuelPrice[form].checked==true)
    {
        fuelPrice=30;
    }
    return fuelPrice;
}

function withPol(form)
{
    var polishPrice=0;
    var theForm = document.forms["price"];
    var includeInscription = document.getElementsByClassName("pol");
    if(includeInscription[form].checked==true){
        polishPrice=50;
    }
    return polishPrice;
}

function driver(form)
{
    var driverPrice=0;
    var theForm = document.forms["price"];
    var getDriv = document.getElementsByClassName("drv");
    if(getDriv[form].checked==true){
        driverPrice=50;
    }
    return driverPrice;
}

function calculateTotal(form)
{
var car1= 50
    var total = fuelPrice(form) + withPol(form) + car1 + driver(form);
     var text = "Total Price For the Renting "+total+ "BHD/Week";
    //display the result
    var divobj = document.getElementsByClassName('totalPrice');
    divobj[form].style.display='block';
    divobj[form].innerHTML = text;
  
    return text;

}

function myFunction()
{
	var name = calculateTotal()
	confirm(name)
}
&#13;
&#13;
&#13;