我的网格视图中有一些下拉列表,每个都有2个相同的选项,我想计算所有这些下拉列表中选择的option1次数(selectedIndex = 1)以及选择第二个选项的次数(selectedIndex = 2)使用jQuery。
<asp:GridView ID="gd1" runat="server" AutoGenerateColumns="False"
onrowdatabound="gd1_RowDataBound" >
<Columns>
<asp:BoundField DataField="id" Visible="False"/>
<asp:BoundField DataField="fullName" Visible="True"
HeaderText="Full Name"/>
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="ddl2" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="ddl3" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Count1" runat="server" Text="First Count"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="CountSelected1" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Count2" runat="server" Text="Second Count"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="CountSelected2" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在最后2列中,我想显示option1和option2的总选择。
答案 0 :(得分:1)
第一个选项: 您可以声明两个全局变量
var first=0;
var second=0;
然后执行以下操作:
$(function(){
$("#selectBox").change(function(){
switch($(this).val())
{
case "0": first++;
break;
case "1": second++;
break;
}
});
});
为复选框编写此函数:
<select id="selectBox">
<option value="0" id="first">FirstOption</option>
<option value="1" id="second">SecondOption</option>
</select>
第一种选择不是很好但是快速解决方案。
第二个选项:
您可以创建可以计算选择的jQuery小部件,然后使用此小部件包装复选框。这是widget的例子(我从某人的博客中复制了它但丢失了链接以获取源代码,希望我的例子有用)
<script type="text/javascript">
var Green = {
// Set up the widget
_create: function () {
},
_init: function () {
this.setLevel(this.getLevel());
},
getLevel: function () { return this.options.level; },
setLevel: function (x) {
var greenLevels = this.options.greenLevels;
var level = Math.floor(Math.min(greenLevels.length - 1, Math.max(0, x)));
this.options.level = level;
this.element.css({ background: greenLevels[level] });
},
options: {
level: 5,
greenLevels: ['#000', '#010', '#020', '#030', '#040', '#050', '#060', '#070', '#080', '#090', '#0a0', '#0b0', '#0c0', '#0d0', '#0e0', '#0f0', '#fff']
},
darker: function () {
this.setLevel(this.getLevel() - 1);
},
lighter: function () {
this.setLevel(this.getLevel() + 1);
},
off: function () {
debugger;
this.element.css({ background: 'none' });
this.destroy();
},
_setOptions: function () {
$.Widget.prototype._setOptions.apply(this, arguments);
this._refresh();
}
};
$(function () {
(function ($, undefined) {
$.widget('ui.green', Green);
})(jQuery);
});
function on() {
$('.targetDiv').green(
{ level: 8,
greenLevels: ['#000', '#00f', '#088', '#0f0', '#880', '#f00', '#808', '#fff']
});
}
</script>
<p class="targetDiv">
This is a test div text.</p>
<input type="button" value="On" onclick="return on()" />
<input type="button" value="Lighter" onclick="$('.targetDiv').green('lighter');" />
<input type="button" value="Darker" onclick=" $('.targetDiv').green('darker');" />
<input type="button" value="Off" onclick=" $('.targetDiv').green('off');" />
答案 1 :(得分:1)
使用RowDataBound事件在DropDown列表的Onchange事件上声明Javascript函数为Follow ...
protected void grd_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropdownList drpLst = ((DropdownList)e.Row.FindControl("ddl1"));
Label lblCount2= (Label)e.Row.FindControl("Count2");
drpLst.Attributes["onchange"]="calculate(this,'"+lblCount2.ClientID+"');"
}
}
<强>使用Javascript:强>
var count=0;//This should be Global
function calculate(drpDownList,labelId)
{
if(drpDownList.selectedIndex==0)
{
count=count+1;
}
labelId.innerHTML=count;
}
答案 2 :(得分:0)
我解决了我的问题我的解决方案here
有效的最终代码:
var collection = $('select.ddlJ');
for (var element in collection)
$(element).change(function(){})
$(function() {
$('select.ddlJ').change(function(e) {
$(this).parent().parent().find('td:last').prev().find('span').html(
$(this).parent().parent().find( 'select.ddlJ' ).filter(function() {
return $.trim($(this).val()) == 'm';
}).length
);
$(this).parent().parent().find('td:last span').html(
$(this).parent().parent().find( 'select.ddlJ' ).filter(function() {
return $.trim($(this).val()) == 'n';
}).length
);
});
});