可能的解决办法,但无法让它发挥作用:
$(function(){
$('input[name="q10"]').click(function(){
if ($(this).is(':checked'))
{
var flag=0;
$("input[type='radio']:checked").each(function() {
if($(this).val()==1 || $(this).val()==2 ){
$("#disagree").show();
flag++;
}
else if(($(this).val()==3 || $(this).val()==4) && flag==0)
$("#agree").show();
});
}
});
});
我有一个包含10个问题的页面 - 每个问题的响应比例如下:
<div id="rq1" class="row" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<div class="col1">1.</div>
<div class="col2a">Question text</div>
<div class='colans'>
<input name='q1' value='4' id='q1a4' type='radio' onclick='mand();' />
</div>
<div class='colans'>
<input name='q1' value='3' id='q1a3' type='radio' onclick='mand();' />
</div>
<div class='colans'>
<input name='q1' value='2' id='q1a2' type='radio' onclick='mand();' />
</div>
<div class='colans'>
<input name='q1' value='1' id='q1a1' type='radio' onclick='mand();' />
</div>
<div class='colans'>
<input name='q1' value='-1' id='q1a-1' type='radio' onclick='mand();' />
</div>
</div>
以上复制了q1至q10的10个问题。
在10个问题的最后,我有两个textareas
用户可以在其中撰写评论,但是,如果用户选择 ANY 响应,则需要显示第一个textarea
值为2或1时,如果用户选择值为4或3的 ANY 响应但未选择带值的响应,则需要显示第二个textarea
2或1。
最后,我需要在用户回答完最后一个问题后运行jQuery,问题10 - 此时我需要触发代码。
过去,我已为每个响应手动创建了Javascript代码并进行了检查,但我想知道是否有更整洁,更简洁的方式使用jQuery?
答案 0 :(得分:1)
好吧,我没有很好地格式化代码,但我想这会对你有所帮助。我也检查了最后给出的答案,我猜每次访问这个对象都不是那个好主意。但这只是我的看法。一切顺利: - )
`var myradio = $('input[name=nettype]');
var nettype = myradio.filter(':checked').val();
if(nettype ==1 || nettype ==2 )
{
$("#myTextArea1").show();
$("#myTextArea2").hide();
}
else if(nettype ==3 || nettype == 4 )
{
$("#myTextArea2").show();
$("#myTextArea1").hide();
}`
对于您已添加到上一个答案的评论,您可以添加以下代码
`if ( $('.myClass').filter(function(){
return $(this).val() != '';
}).length == 0
)
{
/* code to run when all are empty */
}`
你可以将这个类名myClass添加到你的文本框中,这样当所有文本框都被回答时,你可以为radiobutton激活代码。 : - )
答案 1 :(得分:0)
var flag=0;
$("input[type='radio']:checked").each(function() {
if($(this).val()==1 || $(this).val()==2 ){
$("#myTextArea1").show();
flag++;
}
else if(($(this).val()==3 || $(this).val()==4) && flag==0)
$("#myTextArea2").show();
});
在最后一个问题得到解答时触发此代码!!
更新: 这是完整的代码...
$(document).ready(function() {
$("#disagree").hide();
$("#agree").hide();
$("input[name='q10']").click(function() {
$("#disagree").hide();
$("#agree").hide();
var flag=0;
$("input[type='radio']:checked").each(function() {
if($(this).val()==1 || $(this).val()==2 ){
$("#disagree").show();
$("#agree").hide();
flag++;
}
else if(($(this).val()==3 || $(this).val()==4) && flag==0)
$("#agree").show();
});
});
});