我在这里获取单选按钮的值时遇到了一些问题:http://testing.feministbitch.com/index.php/quiz_creator。我有完整的代码在最底部。我如何获得这些单选按钮的值?
当你点击添加问题时,我做了让人们可以选择下列单选按钮:true/false或多项选择。
我想要做到这一点:根据他们是否选择真/假或多项选择,进一步的单选按钮或输入+下拉将降低问题的下面。但我失败了。请帮帮我 :)。
这里的问题是,我不知道如何设置var x等于获得true_false值或multiple_choice值。
我试过了,但是这些都不起作用。
//var x = $("#question_type"+count).value;
//var x=document.forms["quizquiz"]["question_type" + count].value;
下面是追加真/假的东西或者多东西基于他们选择什么单选按钮,在我的代码部分:
if (x=="true_false"){
$('#container').append(
'The above statement is '+ '<Input id="true_or_false_' + count + '" type = "Radio" Name ="true_or_false[]" value= "true">' + 'True' + ' '
+ '<Input id="true_or_false_' + count + '" type = "Radio" Name ="true_or_false[]" value= "false">'+ 'False<br /><br/>'
);
}else{
$('#container').append(
'A: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'B: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'C: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'D: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' + '<br />'
+'Correct Choice: '
+'<select name="correct_choice[]">'
+'<option value="A">A</option>'
+'<option value="B">B</option>'
+'<option value="C">C</option>'
+'<option value="D">D</option>'
+'</select>'
+'<br /><br />'
);
}
下面是完整的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Quiz Creator</title>
<script type="text/javascript" src="http://testing.feministbitch.com/js/jquery.js"></script>
<script type="text/javascript">
var count = 0;
$(function(){
$('p#add_field').click(function(){
count += 1;
$('#container').append(
'<strong>Question ' + count + '</strong><br />'
+ 'Question text: ' + '<input id="question_field_' + count + '" name="question_fields[]' + '" type="text" maxlength="300" size="150"/><br />'
+ '<Input id="question_type_' + count + '" type = "Radio" Name ="questiontype_fields'+count+'[]" value= "true_false">' + 'True/False' + ' '
+ '<Input id="question_type_' + count + '" type = "Radio" Name ="questiontype_fields'+count+'[]" value= "multiple_choice">'+ 'Multiple Choice<br /><br/>'
);
//var x = $("#question_type"+count).value;
var x=document.forms["quizquiz"]["question_type" + count].value;
var a=3;
alert(a);
if (x=="true_false"){
$('#container').append(
'The above statement is '+ '<Input id="true_or_false_' + count + '" type = "Radio" Name ="true_or_false[]" value= "true">' + 'True' + ' '
+ '<Input id="true_or_false_' + count + '" type = "Radio" Name ="true_or_false[]" value= "false">'+ 'False<br /><br/>'
);
}else{
$('#container').append(
'A: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'B: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'C: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' +
'D: ' + '<input id="multi_choices_' + count + '" name="multiple_choices[]' + '" type="text" maxlength="300" size="150"/><br />' + '<br />'
+'Correct Choice: '
+'<select name="correct_choice[]">'
+'<option value="A">A</option>'
+'<option value="B">B</option>'
+'<option value="C">C</option>'
+'<option value="D">D</option>'
+'</select>'
+'<br /><br />'
);
}
});
});
</script>
</head>
<body>
<div id="upload">
<h1>Quiz Creator</h1>
<?php
$attributes = array('name' => 'quizquiz', 'id' => 'quizquiz');
echo form_open_multipart('quiz_creator');
//controller named quiz_creator
?>
Quiz Name: <input id="quiz_name" name="quiz_name" type="text" maxlength="100" size="100"/><br /><br />
<?php
//-------------------------------------------------study guide uploader
for ($i = 1; $i <= 5; $i++) {
echo 'Study Guide '.$i.': '. form_upload('studyguide'.$i.'') . "<br />";
}
//-------------------------------------------------study guide uploader
echo "<br />";
?>
<div id="container">
<p id="add_field"><a href="#"><span>» Add a Question</span></a></p>
</div>
<br /><br />
<?php
echo "<br /><br />";
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</body>
</html>
代替'.value'尝试'.VAL()'。应该看起来像这样'$('#question_type“+ count:checked')。val();' – Jeemusu 2012-07-31 03:19:41
如果您为多个'input'标签使用相同的'id',那么您的HTML将会失效并且可能会出现严重问题。 'name'应该与它们相同,但'id'应该是唯一的。看看jQuery文档中的第二个例子是否有帮助:[jQuery:checked selector](http://api.jquery的.com /签选择器/) – TLS 2012-07-31 03:26:11