单选按钮阵列名称 - 如何访问它们

时间:2015-04-16 20:02:00

标签: jquery radio

我有一个带有单选按钮的表单。我需要检查是否单击了单选按钮,但是我的名字有问题。我试着写下answeranswer[],但它没有告诉我下一个问题。如果我写它input[type='radio']:checked我可以这样做吗? :)



$(function () {
        $("#next").click(showNextQuestion);
    });

var currentInd = 0;
var prevInd = 0;
function showNextQuestion(){
$(function () {
	var indId = "#"+currentInd;
	prevInd = currentInd - 1;
	var prevId = "#"+prevInd;
	//validation
	var isAnyClicked = false;
	

	if ($("input[name^='answer']:checked").val()) {
   		isAnyClicked = true;
	}
	if(currentInd > 0 && isAnyClicked == false)
	{
		alert('Otgowori!');
	}
	else{
		//hide previous question
		
		$(prevId).hide();
		
		//show next question 
		var indId = "#"+currentInd;
	        $(indId).show();
	        currentInd++;
    }
    });
}

$data=array(
			'name' =>  'answer['.$row->question_id.']',
			'value' => '5'
		);
echo form_radio($data); 
		echo " 5 ";
		$data=array(
			'name' =>  'answer['.$row->question_id.']',
			'value' => '4'
		);
		echo form_radio($data);
		echo " 4 ";
		$data=array(
			'name' => 'answer['.$row->question_id.']',
			'value' => '3'
			
		);
		echo form_radio($data);
		echo " 3 ";




1 个答案:

答案 0 :(得分:1)

因为你有这样的ids:answer [xxx]我不确定你的选择器是否会起作用。

您可以尝试这样找到一个以值开头的属性(在您的情况下类似于:

input[name^='answer']:checked

虽然我没有测试过。

https://api.jquery.com/attribute-starts-with-selector/