可能的解决方案,但无法使其正常工作:
$(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
,其中用户可以写入注释,但是,第一textarea
需求出现,如果用户选择任何具有2个或1。第二值响应textarea
需求出现如果用户选择任何响应值为4或3,但尚未选择值为2或1的响应。
最后,一旦用户回答了最后一个问题(问题10),我就需要运行jQuery-在这一点上,我需要激发代码。
过去,我为每个响应手动创建了Javascript代码并进行了检查,但我想知道使用jQuery是否有一种更简洁,更简洁的方法?
好吧,我还没有很好地格式化代码,但是我想这将对您有所帮助。我还检查了给出的最后一个答案,我想每次访问该对象都不是一个好主意,但这只是我的观点。一切顺利 :-)
`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添加到您的文本框中,以便当所有文本框都将得到回答时,您可以触发单选按钮的代码。:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句