我在制作ajax无线电验证时遇到问题,我需要的是:我有4个选项,每个选项有2个(左侧和右侧)选项。没有选项必须具有相同的选择(左或右)。问题是:你不能在同一个选项中选择(左和右),如果你点击下一步,它会提示你一个警告错误。我链接了一个图像,以便更好地理解我的意思。
http://i176.photobucket.com/albums/w162/jeffprado/1.png~original
这是我的代码:
<?php foreach($results as $result):?>
<?php
if($i==1){?>
<div id="question<?php echo $i;?>" class="cont">
<?php echo $result['Q1'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M1" id="Q<?php echo $result['ID'];?>M1" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L1" id="Q<?php echo $result['ID'];?>L1" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q2'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M2" id="Q<?php echo $result['ID'];?>M2" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L2" id="Q<?php echo $result['ID'];?>L2" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q3'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M3" id="Q<?php echo $result['ID'];?>M3" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L3" id="Q<?php echo $result['ID'];?>L3" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q4'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M4" id="Q<?php echo $result['ID'];?>M4" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L4" id="Q<?php echo $result['ID'];?>L4" name="<?php echo $result['ID'];?>L"/>
<br/>
<button id='<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>
</div><?php
}
elseif($i<1 || $i<$rows){?>
<div id='question<?php echo $i;?>' class='cont'>
<?php echo $result['Q1'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M1" id="Q<?php echo $result['ID'];?>M1" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L1" id="Q<?php echo $result['ID'];?>L1" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q2'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M2" id="Q<?php echo $result['ID'];?>M2" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L2" id="Q<?php echo $result['ID'];?>L2" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q3'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M3" id="Q<?php echo $result['ID'];?>M3" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L3" id="Q<?php echo $result['ID'];?>L3" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q4'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M4" id="Q<?php echo $result['ID'];?>M4" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L4" id="Q<?php echo $result['ID'];?>L4" name="<?php echo $result['ID'];?>L"/>
<br/>
<button id='<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>
<button id='<?php echo $i;?>' class='next btn btn-success' type='button' >Next</button>
</div><?php
}
elseif($i==$rows){?>
<div id='question<?php echo $i;?>' class='cont'>
<?php echo $result['Q1'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M1" id="Q<?php echo $result['ID'];?>M1" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L1" id="Q<?php echo $result['ID'];?>L1" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q2'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M2" id="Q<?php echo $result['ID'];?>M2" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L2" id="Q<?php echo $result['ID'];?>L2" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q3'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M3" id="Q<?php echo $result['ID'];?>M3" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L3" id="Q<?php echo $result['ID'];?>L3" name="<?php echo $result['ID'];?>L"/>
<br/>
<?php echo $result['Q4'];?>
<input type="radio" value="Q<?php echo $result['ID'];?>M4" id="Q<?php echo $result['ID'];?>M4" name="<?php echo $result['ID'];?>M"/>
<input type="radio" value="Q<?php echo $result['ID'];?>L4" id="Q<?php echo $result['ID'];?>L4" name="<?php echo $result['ID'];?>L"/>
<br/>
<button id='<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>
<button id='<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
</div><?php
} $i++;?>
<?php endforeach;?>
这里是ajax脚本(也许它也有帮助):
<script>
$('.cont').addClass('hide');
count=$('.questions').length;
$('#question'+1).removeClass('hide');
$(document).on('click','.next',function(){
last=parseInt($(this).attr('id'));
nex=last+1;
$('#question'+last).addClass('hide');
$('#question'+nex).removeClass('hide');
});
$(document).on('click','.previous',function(){
last=parseInt($(this).attr('id'));
pre=last-1;
$('#question'+last).addClass('hide');
$('#question'+pre).removeClass('hide');
});
setTimeout(function() {
$("form").submit();
}, 60000);
</script>