使用jquery中的每个语句选择一组下拉框值
我想用速度模板语言(VTL)编写一个简单的代码。我有一组下拉列表,我想迭代所有下拉列表并获取每个下拉列表的值并计算每个不同类别下选定值的数量。代码如下:
#macro( macroYesNoSelect $fieldId $section )
<select name='${fieldId}' id='${fieldId}' section='${section}' ischanged="">
<option value="Yes"
#if ($Form.getFieldValue("${fieldId}") == "Yes")
selected
#end
score="1">Yes</option>
<option value="No"
#if ($Form.getFieldValue("${fieldId}") == "No")
selected
#end
score="0">No</option>
<option value="N/A"
#if ($Form.getFieldValue("${fieldId}") == "N/A")
selected
#end
score="0">N/A</option>
</select>
#end
Javascript:
<script>
calcYesNO("Question");
function calcYesNO(sectionName){
var scoreSum=0;
var optionYes=0;
var optionNo=0;
var optionNA=0;
$("select[section='"+ sectionName +"']").each(function(){
if($(this).find("option:selected").attr('value') == 'Yes')
{
optionYes=optionYes+1;
}
else if($(this).find("option:selected").attr('value') == 'No')
{
optionNo=optionNo+1;
}
else
{
optionNA=optionNA+1;
}
}
scoreSum = optionYes;
return scoreSum;
}
</script>
HTML 代码:
<table>
<tr>
<td>Question1 ?</td>
<td>
#macroYesNoSelect ( "id1" "Questions" )
</td>
</tr>
<tr>
<td> Question2 ?</td>
<td>
#macroYesNoSelect ( "id2" "Questions" )
</td>
</tr>
<tr>
<td> Question3 ?</td>
<td>
#macroYesNoSelect ( "id3" "Questions" )
</td>
</tr>
</table>
我想根据我的选择选择 YES
、NO
和 NA
的数量。但是当我调用函数 calcYesNO()
时,我无法计算它们......
有人可以帮助我解决这个问题吗? 谢谢 基肖尔
I want to write a simple code in Velocity Template Language (VTL). I have a group of dropdowns, I want iterate through all the dropdowns and get the values of each dropdown and count the number of selected values under each distinct category. The code is listed below:
#macro( macroYesNoSelect $fieldId $section )
<select name='${fieldId}' id='${fieldId}' section='${section}' ischanged="">
<option value="Yes"
#if ($Form.getFieldValue("${fieldId}") == "Yes")
selected
#end
score="1">Yes</option>
<option value="No"
#if ($Form.getFieldValue("${fieldId}") == "No")
selected
#end
score="0">No</option>
<option value="N/A"
#if ($Form.getFieldValue("${fieldId}") == "N/A")
selected
#end
score="0">N/A</option>
</select>
#end
Javascript:
<script>
calcYesNO("Question");
function calcYesNO(sectionName){
var scoreSum=0;
var optionYes=0;
var optionNo=0;
var optionNA=0;
$("select[section='"+ sectionName +"']").each(function(){
if($(this).find("option:selected").attr('value') == 'Yes')
{
optionYes=optionYes+1;
}
else if($(this).find("option:selected").attr('value') == 'No')
{
optionNo=optionNo+1;
}
else
{
optionNA=optionNA+1;
}
}
scoreSum = optionYes;
return scoreSum;
}
</script>
HTML code:
<table>
<tr>
<td>Question1 ?</td>
<td>
#macroYesNoSelect ( "id1" "Questions" )
</td>
</tr>
<tr>
<td> Question2 ?</td>
<td>
#macroYesNoSelect ( "id2" "Questions" )
</td>
</tr>
<tr>
<td> Question3 ?</td>
<td>
#macroYesNoSelect ( "id3" "Questions" )
</td>
</tr>
</table>
I want to select the number of YES
, NO
and NA
as per my selection. But when I call the function calcYesNO()
, I am unable to calculate them...
Can someone help me on resolving this?
Thanks
Kishore
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 在检索
也就是说,您现有的代码看起来应该可以工作。您是否已验证 $("select[section='"+sectionName +"']") 实际上正在检索选择菜单?
jQuery is smart about retrieving <select> values; you can call .val() directly. I also recommend being more explicit with the each() iterator, since the "this" keyword can have unexpected values in certain circumstances.
That said, your existing code looks like it should work. Have you verified that $("select[section='"+ sectionName +"']") is actually retrieving the select menu?