选中复选框时如何调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7612218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call a javascript function when Checkbox is checked
提问by Lucas_Santos
How can I call a Javascript function when a checkbox is checked when this checkbox is inside a gridview ?
当复选框位于 gridview 中时,如何在选中复选框时调用 Javascript 函数?
protected void AlteraStatusExpiraSeteDias_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdImoveis2.Rows.Count; i++)
{
GridViewRow RowViewExpiraSeteDias = (GridViewRow)grdImoveis2.Rows[i];
CheckBox chk = (CheckBox)grdImoveis2.Rows[i].FindControl("chkExpiraSeteDias");
if (chk != null)
{
String codigo;
if (chk.Checked)
{
codigo = (String)grdImoveis2.Rows[i].Cells[0].Text;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registra", "AlteraStatus(codigo);", false);
}
}
}
}
<asp:GridView ID="grdImoveis2" CssClass="StyleGrid" Width="100%" runat="server" AutoGenerateColumns="false" DataSourceID="ds" BorderWidth="0" GridLines="None">
<AlternatingRowStyle BackColor="White" CssClass="EstiloDalinhaAlternativaGrid" HorizontalAlign="Center"/>
<RowStyle CssClass="EstiloDalinhaGrid" HorizontalAlign="Center" />
<HeaderStyle BackColor="#e2dcd2" CssClass="thGrid" Height="20" />
<Columns>
<asp:BoundField HeaderText="Código" DataField="Imovel_Id" />
<asp:BoundField HeaderText="Para" DataField="TransacaoSigla" />
<asp:BoundField HeaderText="Valor" DataField="ValorImovel" DataFormatString="{0:c}" HtmlEncode="false" />
<asp:TemplateField HeaderText="Endereco">
<ItemTemplate>
<%# Eval("Logradouro") %>, <%# Eval("Numero") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Data Cadastro" DataField="DataHora" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"/>
<asp:BoundField HeaderText="Data Expira" DataField="DataExpira" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"/>
<asp:TemplateField HeaderText="A??o">
<ItemTemplate>
<asp:CheckBox ID="chkExpiraSeteDias" runat="server" onclick="alert('Foo')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Without the checkbox, when I put a image and put a link href to the javascript, it's works!, but with checkbox, no!
没有复选框,当我放一张图片并在 javascript 上放一个链接 href 时,它的工作原理!但有了复选框,不!
回答by Bala R
Add onclick
attribute to Checkbox markup and include the javascript function that you'd like to call.
将onclick
属性添加到复选框标记并包含您要调用的 javascript 函数。
<asp:CheckBox ID="chkExpiraTresDias" runat="server" onclick="alert('Foo')" />
回答by Junaid
this should help you
这应该可以帮助你
$('input:checkbox[ID$="chkExpiraTresDias"]').change(function() {
alert('Hello world!');
});
$('input:checkbox[ID$="chkExpiraTresDias"]').change(function() {
alert('Hello world!');
});
回答by Hanlet Esca?o
I fear that you will have to iterate through the Gridview when your "Insert" button is clicked and then do what you have to do. Something like this:
我担心当您单击“插入”按钮时,您将不得不遍历 Gridview,然后执行您必须执行的操作。像这样的东西:
foreach (GridViewRow row in this.grdImoveis2.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
CheckBox chk = row.Cells(0).FindControl("chkExpiraTresDias");
if (chk != null) {
Response.Write(chk.Checked); //Do what you gotta do here if it's checked or not
}
}
}
Good luck.
祝你好运。
Hanlet
汉莱特