好的,所以我正在为 FAQ 页面创建一个调查,而我的 jQuery 将无法识别输入或标签标签中的 id。这是我的 jQuery:
$(document).ready(function() {
$('.rating').click(function() {
$('.rating').removeClass('selected');
ratingClick(this);
});
});
function ratingClick(that) {
console.log(that.id);
if (that.id == 'rating4' || that.id == 'rating5') {
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
$form.replacewith("VERY NICE");
},'json');
return false;
});
}
else {
$('#getMore').fadeIn();
$(that).toggleClass('selected');
}
}
$(document).ready(function(){
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
$form.replacewith("VERY NICE");
},'json');
return false;
});
});
如您所见,如果此人选择评分 4 或 5,我会尝试提交表单。当我单击 4 或 5 时,它会显示 #getMore div,而不是将其提交到服务器并替换表单与“非常好”。这是表单的 HTML:
<body>
<div id = "form">
<form id="questions" action="connect.php" method="post">
<h2>How helpful is this article?</h2>
<div class="ratings">
<input type="radio" name="Q1" id="rating1" value="1"><label class="rating" for="rating1">Not at all helpful</label>
<input type="radio" name="Q1" id="rating2" value="2"><label class="rating" for="rating2">Not very helpful</label>
<input type="radio" name="Q1" id="rating3" value="3"><label class="rating" for="rating3">Somewhat helpful</label>
<input type="radio" name="Q1" id="rating4" value="4"><label class="rating" for="rating4">Very helpful</label>
<input type="radio" name="Q1" id="rating5" value="5"><label class="rating" for="rating5">Extremely helpful</label>
</div>
<div id="getMore">
<h2>Please tell us why you didn't find this article helpful:</h2>
<input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
<input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
<input type='checkbox' name="Q2_3" value="1">Too much information<br/>
<input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
<input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
<input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>
<h2>Do you have any other feedback about this article?</h2>
<p><input type="text" name="Q3" /><p>
<div id = "submit"><input type='submit' value="Submit" /></div>
</div>
</form>
</div>
</body>
如何使用 id 来定位这些单选按钮中的一个,或者是否有另一种方法可以在不使用 id 的情况下做到这一点?
编辑:JFIDDLE 链接http://jsfiddle.net/amccoy022/uN7zv/