0

我正在制作一个带有多个单选按钮的调查表。问题在数据库中,我选择问题并将选项显示为单选按钮

<div id="question">
    <form id="pollform">;
<?php
    $i=0;
    while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
        $i++; 
?>
        <p><?php echo $ID.") ".$Question; ?></p>
        <input type="radio" name="radio<?php echo $i;?>" value="1" /> <?php echo $one; ?><br />
        <input type="radio" name="radio<?php echo $i;?>" value="2" /> <?php echo $two; ?><br />
        <input type="radio" name="radio<?php echo $i;?>" value="3" /> <?php echo $three; ?><br />
        <input type="radio" name="radio<?php echo $i;?>" value="4" /> <?php echo $four; ?><br />
        <input type="radio" name="radio<?php echo $i;?>" value="5" /> <?php echo $five; ?><br />
<?php 
    } 
?>
    <input type="button" id="review-submit" value="Submit" onclick="pollsubmit();">
    <form>
</div>

为了处理这个并使用 Ajax 发送选定的答案,我使用了以下代码

<script language=Javascript>
function pollsubmit(){
<?php
    $dbquerry="SELECT `ID`, `Question`, `Form`, `one`, `two`,`three`,`four`,`five` FROM `pollquestions` Order by `ID`";
    $result=mysql_db_query($dbname,$dbquerry);
?>
    var answers = new Array();
    var form1=document.getElementById("pollform");
<?php
    $count=0;
    while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
        $count++;
?>
        for (var i=1;i<=5;i++) {
            if(form1.radio<?php echo $count;?>[i].checked) {
                answers[<?echo $ID;?>]= form1.radio<?php echo $count;?>.value;
            }
        }
<?php 
    }
?>
}
</script>

但这让我Cannot read property 'checked' of undefined在这两个地方都有一个名字是radio1。而如果我只使用字符串,例如name="radio1"它正在工作,但如果我使用name="radio<?php echo $i;?>"它则不起作用。你能帮忙吗

4

2 回答 2

0

javascript for 循环应该从 0 开始。对于这个值,你需要一个 [i]。

for (var i=0;i<5;i++) {
    if(form1.radio<?php echo $count;?>[i].checked) {
        answers[<?echo $ID;?>]= form1.radio<?php echo $count;?>.value;
    }
}
于 2012-04-14T09:45:49.120 回答
0
<script language=Javascript>
function pollsubmit(){
<?php
    $dbquerry="SELECT `ID`, `Question`, `Form`, `one`, `two`,`three`,`four`,`five` FROM `pollquestions` Order by `ID`";
    $result=mysql_db_query($dbname,$dbquerry);
?>
    var answers = new Array();
    var form1=document.getElementById("pollform");
<?php
    $count=0;
    while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
        $count++;
?>
        for (var i=1;i<=5;i++) {
            if(form1.['radio<?php echo $count;?>'][i].checked) {
                answers[<?echo $ID;?>]= form1.['radio<?php echo $count;?>'].value;
            }
        }
<?php 
    }
?>
}
</script>

编辑:

for (var i=1;i<=5;i++) {
                if(form1.['radio<?php echo $count;?>'][i].checked) {
                    answers[<?echo $ID;?>]= form1.['radio<?php echo $count;?>'].value;
                }
            }
于 2012-04-14T12:24:18.367 回答