Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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 复选框和单选按钮。价值观_Javascript_Html_Checkbox_Radio Button - Fatal编程技术网

Javascript 复选框和单选按钮。价值观

Javascript 复选框和单选按钮。价值观,javascript,html,checkbox,radio-button,Javascript,Html,Checkbox,Radio Button,我已经有了这个,但不知道如何进一步。 当我选中2个单选按钮上方的复选框时,它应该查看是否选中了该复选框,然后当我选中1个单选按钮时,它应该选中该单选按钮并将该值添加到总值中 <form name="CalorieForm"> <input type="checkbox" name="gf"> Groente en fruit <br /> &nbsp <input type="radio" name="gf1" va

我已经有了这个,但不知道如何进一步。 当我选中2个单选按钮上方的复选框时,它应该查看是否选中了该复选框,然后当我选中1个单选按钮时,它应该选中该单选按钮并将该值添加到总值中

     <form name="CalorieForm">
    <input type="checkbox" name="gf"> Groente en fruit <br />
      &nbsp <input type="radio" name="gf1" value="60"> Appel (60 kcal per appel) <br />
      &nbsp <input type="radio" name="gf2"value="26"> Paprika (26 kcal per paprika) <br />
    <br>
    <input type="checkbox" name="bpp"> Brood, pasta en peelvruchten <br />
      &nbsp <input type="radio" name="bpp1" value="111"> Zilvervliesrijst (111 kcal per 100 gram) <br />
      &nbsp <input type="radio" name="bpp2" value="24"> Sperziebonen(24 kcal per 100 gram)<br />
    <br>
    <input type="checkbox" name="zvvev"> Zuivel, vlees, vis, ei en vleesvervangers <br />
      &nbsp <input type="radio" name="zvvev1" value="118"> Kabeljauwfilet (118 kcal per 100 gram) <br />
      &nbsp <input type="radio" name="zvvev2" value="115"> Biefstuk (115 kcal per 100 gram) <br />
    <br>
    <input type="checkbox" name="vo"> Vetten en olie <br />
      &nbsp <input type="radio" name="vo1" value="108"> Olie (108 kcal per eetlepel) <br />
      &nbsp <input type="radio" name="vo2" value="90"> Halvarine (90 kcal per 25 gram) <br />
    <br>
    <input type="checkbox" name="v"> Vocht <br />
      &nbsp <input type="radio" name="v1" value="0"> Thee (0 kcal per 0.5 liter) <br />
      &nbsp <input type="radio" name="v2" value="0.6"> Coca cola light (0.6 kcal per blikje) <br />
    <br>
    <input type="button" value="Bereken de calorieën" name="totaal" onClick="BerekeningCalorie()"> <input type="reset" value="Reset" />
    <br>
    <br>
    Aantal calorieën: <input type="text" name="Calorie" size="20"><br />
 </form>
我问我的老师怎么做,她说应该是这样,这是我从她给我看的东西中记得的


谁能帮我修一下吗?我不知道如何继续下去。javascript noob.

这里的代码有一些非常基本的错误

[HTML]-我假设您希望单选按钮在其集合中一起工作。例如:现在只选择gf1或gf2,您可以同时选择两者。你根据他们的名字来做,所以他们都必须是gf1 [HTML]-按钮onclick大写,但JS不是BerekeningCarrie vs BerekeningCarrie [JavaScript]-语法上,所有if条件都需要括号。 [JavaScript]-getElementByName不是一个方法。getElementByNames为复数,但返回元素集合。您可以为它们提供所有id属性并使用getElementById,或者只使用document.formName.inputName,就像您对值document.carrioform.gf1所做的那样 [JavaScript]-您的计算应为总计=总计+加法或总计+=加法;您当前的总数+=总数+加起来是总数的两倍,然后再加上额外的数字。 [JavaScript]-输入值实际上是字符串,而不是数字。因此,您需要使用parseInt以数字方式添加它: [JavaScript]-您没有输出值,我假设这就是[name=Carolie]输入的目的。 [JavaScript]-另外,为了更好的衡量,我假设你意识到你没有计算 您可以在此处看到固定的:

HTML:


totaal+=totaal+document.carorieform.bpp2.value应该是:totaal+=document.carorieform.bpp2.value或者totaal=totaal+document.carorieform.bpp2.valueOh,应该是total=total+addition。我使用了您的示例,但仍然不起作用。@user3470213您是对的,您的代码还有很多错误。我已经更新了我的答案。如果我的回答对你有帮助,记得接受它:我想我们已经接近我想要的了。它现在可以工作了,但应该只有一个单选按钮可选择的2。两者的值gf1应为60,gf2应为26。此外,如果未选中该复选框,则复选框下选定的单选按钮不应为总数提供任何值。所以当选中复选框和gf1时,则总数为+60。如果勾选gf2,则总数+26。如果未选中复选框,则总数+0。此外,这应该适用于所有5个复选框。所以当我选择GF1+bpp1时,它应该得到+60+111Yup,这就是我的代码的工作方式。你检查过JSFIDLE链接了吗?
        function berekeningCalorie() {
var totaal = 0
if document.getElementByName("gf").checked {
 if document.getElementByName("gf1").checked 
  totaal += totaal + document.CalorieForm.gf1.value
 if document.getElementByName("gf2").checked 
  totaal += totaal + document.CalorieForm.gf2.value
 }

if document.getElementByName("bpp").checked {
 if document.getElementByName("bpp1").checked 
  totaal += totaal + document.CalorieForm.bpp1.value
 if document.getElementByName("bpp2").checked 
  totaal += totaal + document.CalorieForm.bpp2.value
 }
}
<form name="CalorieForm">
  <input type="checkbox" name="gf"> Groente en fruit <br />
  <input type="radio" name="gf1" value="60"> Appel (60 kcal per appel) <br />
  <input type="radio" name="gf1" value="26"> Paprika (26 kcal per paprika) <br />
  <br>
  <input type="checkbox" name="bpp"> Brood, pasta en peelvruchten <br />
  <input type="radio" name="bpp1" value="111"> Zilvervliesrijst (111 kcal per 100 gram) <br />
  <input type="radio" name="bpp1" value="24"> Sperziebonen(24 kcal per 100 gram)<br />
  <br><br>
  <input type="button" value="Bereken de calorieën" name="totaal" onClick="BerekeningCalorie()"> <input type="reset" value="Reset" />
  <br><br>
  Aantal calorieën: <input type="text" name="Calorie" size="20">
 </form>
function BerekeningCalorie() {
  var totaal = 0;

  // If our gf checkbox is checked, then get the value of the radio buttons.
  // Each have the name 'gf1' so it doesn't matter which is checked.
  // In case neither is checked, we default to 0.
  // Then we use parseInt to calulate the number and add it to totaal
  // otherwise, we would be concatenating two strings, not adding two numbers
  if(document.CalorieForm.gf.checked)
    totaal += parseInt(document.CalorieForm.gf1.value || 0, 10);

  // Do the same for the next, etc.
  if(document.CalorieForm.bpp.checked)
    totaal += parseInt(document.CalorieForm.bpp1.value || 0, 10);

  document.CalorieForm.Calorie.value = totaal;
}