我在JS中進行調查,我試圖通過每次選擇第一個選項來顯示在我的頁面上的「kaas」,但它給了我這個錯誤:Uncaught TypeError:無法讀取未定義的屬性'naam'
「遺漏的類型錯誤:無法讀取的未定義的屬性‘NAAM’」
我該如何解決這個問題?
document.write('<form id="form" name="enquete"><p>Zoekt u naar een mobiel product of naar een alles-in-1 pakket?</p><input type="radio" name="naam" id="1">Een mobiel pakket<br><input type="radio" name="naam" id="2">Een alles in 1 pakket<br><input type="radio" name="naam" id="3">Beide<br><br><input type="button" onclick="functie()" value="Submit"></form>');
function functie()
{
var Instap = 0;
var Standaard = 0;
var Premium = 0;
var Internet = 0;
var ZInstap = 0;
var ZStandaard = 0;
var ZPremium = 0;
var Budget100 = 0;
var Budget300 = 0;
var Basis = 0;
var VInternetInstap = 0;
var VInternetStandaard = 0;
document.getElementById('form').submit();
for (var i = 0; i < document.enquete.naam.length; i++)
{
if (document.enquete.naam[i].checked)
{
switch(i)
{
case 0:
{
//Mobiel pakket
document.write('<form id="form2" name="enquete2"><p>Hoeveel belminuten wilt u per maand?</p><input type="radio" name="naam2" id="1">100<br><input type="radio" name="naam2" id="2">150<br><input type="radio" name="naam2" id="3">200<br><input type="radio" name="naam2" id="4">300<br><input type="radio" name="naam2" id="5">Onbeperkt<br><br><input type="button" onclick="" value="Submit"></form>');
//document.getElementById('form2').submit();
if (document.enquete2.naam2[i].checked)
{
switch(i)
{
case 0:
ZInstap = ZInstap + 1;
ZStandaard = ZStandaard +1;
ZPremium = ZPremium + 1;
document.write("kaas");
break;
case 1:
Basis = Basis + 1;
VInternetInstap + 1;
break;
case 2:
VInternetStandaard = VInternetStandaard + 1;
break;
case 3:
Budget300 = Budget300 + 1;
break;
case 4:
ZInstap = ZInstap + 1;
ZStandaard = ZStandaard + 1;
ZPremium + ZPremium + 1;
break;
}
}
}
break;
/*
Standaard = Standaard + 1;
Premium = Premium + 1;
Internet = Internet + 1;
*/
case 1:
{
//Alles in 1
document.write("Alles in 1");
break;
}
case 2:
{
//Beide
break;
}
}
}
}
}
爲什麼你認爲** ** document.enquete.naam應該存在? –
要確定案例選擇的值 –
不,我明白你想做什麼,但我不認爲,你可以像這樣獲取表單的值 - document.formName.inputName.attribute。如果它能工作,那將非常棒。對於這樣的事情,我會使用jQuery和jQuery選擇器$('form [name = enquete] input [name = naam]')。attr('checked');我不知道如何在普通的JavaScript中做這樣的事情,但我幾乎可以肯定你不能按照你嘗試的方式去做。 –