Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getElementsByClassName问题,当我想创建另一个表单时_Javascript_Dom - Fatal编程技术网

Javascript getElementsByClassName问题,当我想创建另一个表单时

Javascript getElementsByClassName问题,当我想创建另一个表单时,javascript,dom,Javascript,Dom,最近,我问了一个关于confirm的问题,问题已经解决了,但现在的问题是,当我尝试为类创建代码时,代码只针对ID,它只针对第一种形式而不是第二种形式进行了更改 函数fuelPrice() { var fuelPrice=0; var theForm=document.forms[“price”]; var withFuelPrice=document.getElementsByClassName(“ful”); 如果(withFuelPrice[0]。选中==true) { 燃料价格=30;

最近,我问了一个关于confirm的问题,问题已经解决了,但现在的问题是,当我尝试为类创建代码时,代码只针对ID,它只针对第一种形式而不是第二种形式进行了更改

函数fuelPrice()
{
var fuelPrice=0;
var theForm=document.forms[“price”];
var withFuelPrice=document.getElementsByClassName(“ful”);
如果(withFuelPrice[0]。选中==true)
{
燃料价格=30;
}
退货价格;
}
函数withPol()
{
var-polishPrice=0;
var theForm=document.forms[“price”];
var includeInscription=document.getElementsByClassName(“pol”);
if(includeInscription[0]。选中==true){
波利斯价格=50;
}
退货价格;
}
函数驱动程序()
{
var-driverPrice=0;
var theForm=document.forms[“price”];
var getDriv=document.getElementsByClassName(“drv”);
if(getDriv[0]。选中==true){
驾驶员价格=50;
}
返回驱动器价格;
}
函数计算器总计()
{
var car1=50
var total=燃料价格()+燃料油()+汽车1+驾驶员();
var text=“租赁总价”+总价+“每周行李处理费”;
//显示结果
var divobj=document.getElementsByClassName('totalPrice');
divobj[0].style.display='block';
divobj[0].innerHTML=text;
返回文本;
}
函数myFunction()
{
var name=calculateTotal()
确认(姓名)
}

每周租赁50 BHD的总价

使用燃油
抛光2周
驱动程序
添加到购物车

每周租赁50 BHD的总价

使用燃油
抛光2周
驱动程序

添加到购物车
查看我是否理解

你应该把表格分开。检查此代码

功能价格(表格)
{
var fuelPrice=0;
var theForm=document.forms[“price”];
var withFuelPrice=document.getElementsByClassName(“ful”);
如果(withFuelPrice[form]。选中==true)
{
燃料价格=30;
}
退货价格;
}
函数与POL(表格)
{
var-polishPrice=0;
var theForm=document.forms[“price”];
var includeInscription=document.getElementsByClassName(“pol”);
if(includeInscription[form].checked==true){
波利斯价格=50;
}
退货价格;
}
函数驱动程序(表单)
{
var-driverPrice=0;
var theForm=document.forms[“price”];
var getDriv=document.getElementsByClassName(“drv”);
if(getDriv[form].checked==true){
驾驶员价格=50;
}
返回驱动器价格;
}
函数计算器总计(表格)
{
var car1=50
var总计=燃料价格(表格)+燃料价格(表格)+汽车1+驾驶员(表格);
var text=“租赁总价”+总价+“每周行李处理费”;
//显示结果
var divobj=document.getElementsByClassName('totalPrice');
divobj[form].style.display='block';
divobj[form].innerHTML=text;
返回文本;
}
函数myFunction()
{
var name=calculateTotal()
确认(姓名)
}

每周租赁50 BHD的总价

使用燃油
抛光2周
驱动程序

每周租赁50 BHD的总价

使用燃油
抛光2周
驱动程序

这是因为您总是访问元素列表中的第一个元素:
withFuelPrice[0]
getDriv[0]
,等等。因此我需要正确地执行循环,我将以我仍然使用的方式发布我的循环beginner@SayedHasan如果你不想做循环检查我的答案。如果你想要动态函数tellme,我可以编辑我的答案是的,它工作了,但计算是在一个确认函数中完成的,所以我需要进行循环或复制粘贴