我有一个代码,用于在点击按钮时验证GridView
内的文本框。
这是一段代码:
function ValidateEmail(btn_id)
{
alert(btn_id.id);
$('#<%=lblTest.ClientID%>').text(btn_id.id);
var btnId = btn_id.id;
alert($(btnId).closest("tr").id);
return false;
}
我通过以下方式在Grid
内调用此方法:
<asp:Button ID="btnEmailUpdate" Text = "Update" CommandName="UpdateEmail" runat="server" onClientClick="return ValidateEmail(this)"/>
在我尝试获取最接近的tr
元素之前,一切看起来都很好:
alert($().closest("tr").id);
我的alert
框显示“未定义”消息。
我在这里做错了什么?
这是我的page.aspx with gridview:
<%@ Page Language="C#" MasterPageFile="~/Portal.master" AutoEventWireup="true" validateRequest="false" enableEventValidation="false" viewStateEncryptionMode ="Never" CodeFile="ISOSearch.aspx.cs" Inherits="ISOProcessing_ISOSearch" Title="ISO Search" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script src="../shared/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ValidateEmail(btnObj)
{
alert(btnObj);
alert(btnObj.id);
alert($(btnObj).closest("tr").attr("id"));
return false;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentLeft" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentMain" Runat="Server">
<asp:UpdatePanel ID="ISOSearchUpdatePanel" runat="server">
<ContentTemplate>
<div id="divIsoSelect" style="width:700px; height:60px; background-color:white">
<fieldset>
<legend style="font-size: larger; font-weight: bold">Search ISO by</legend>
<table>
<tr>
<td><asp:DropDownList ID="ddlISOSearch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlISOSearch_SelectedIndexChanged">
<asp:ListItem Value="">Select...</asp:ListItem>
<asp:ListItem Value="ISO Number" >ISO Number</asp:ListItem>
<asp:ListItem Value="Name">Name</asp:ListItem>
<asp:ListItem Value="Region" >Region</asp:ListItem>
<asp:ListItem Value="Division">Division</asp:ListItem>
</asp:DropDownList></td>
<td > </td>
<td>
<asp:Panel ID="pnlSearchEntry" runat="server" Visible="false" style="width:500px; border:gainsboro; border-width: thick;">
<table>
<tr>
<td valign="middle" style="width:120px"><asp:Label ID="lblEntry" runat="server" Text=""></asp:Label></td>
<td valign="top"><asp:TextBox ID="txtSearchValue" runat="server"></asp:TextBox></td>
<td valign="top"> <asp:Button ID="btnFind" runat="server" Text="Select" OnClick="btnFind_Click"/></td>
</tr>
</table>
</asp:Panel>
</td>
</tr>
</table>
</fieldset>
</div>
<div>
<p>
<asp:Label ID="lblTest" runat="server" style="position:absolute; left: 221px; top: 100px;"></asp:Label></p>
<asp:Label ID="lblMsg" runat="server" style="position:absolute; left: 221px; top: 105px;"></asp:Label></p>
</div>
<asp:GridView ID="grvIsoSearchResults" runat="server"
AutoGenerateColumns="False" PageSize="15"
AllowPaging = "true" Visible="true" BorderColor="Red" GridLines="Both"
OnRowCommand="grvIsoSearchResults_RowCommand"
DataKeyNames="isonum"
OnPageIndexChanging="grvIsoSearchResults_PageIndexChanging"
style="position:absolute; top: 120px;">
<PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PageButtonCount="100" />
<Columns>
<asp:TemplateField HeaderText="ISONUM" SortExpression="isonum">
<ItemTemplate>
<asp:Label ID="txtgvIsoNum" Text = '<%# Eval("ISONUM")%>' runat="server" Width="70px" style="text-align:center" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OFFICE NAME" SortExpression="isoofficename">
<ItemTemplate>
<asp:Label ID="txtgvIsoOfficeName" Text = '<%# Eval("ISOOFFICENAME")%>' runat="server" Width="200px" style="text-align:center" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="REGION" SortExpression="region">
<ItemTemplate>
<asp:Label ID="txtgvRegion" Text = '<%# Eval("REGION")%>' runat="server" Width="50px" style="text-align:center" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DIVISION" SortExpression="client_id">
<ItemTemplate>
<asp:Label ID="txtgvDivision" Text = '<%# Eval("CLIENT_ID")%>' runat="server" Width="50px" style="text-align:center" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMAIL ADDRESS">
<ItemTemplate>
<asp:TextBox ID="txtgvEmailAddress" Text = '<%# Eval("EMAIL")%>' runat="server" Width="200px" />
<asp:Button ID="btnEmailUpdate" Text = "Update" runat="server" CommandName="UpdateEmail" onClientClick="return ValidateEmail(this)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<pagerstyle backcolor="#005482" ForeColor="White" height="30px" verticalalign="Bottom" horizontalalign="Center"/>
</asp:GridView>
<asp:ValidationSummary ID="validationSummary" runat="server" ShowMessageBox="false" ShowSummary="true"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Please Wait.....
</ProgressTemplate>
</asp:UpdateProgress>
</asp:Content>
答案 0 :(得分:1)
尝试:
<table>
<tr id="stack">
<td><input type=button value="Go" id="test" /></td>
</tr>
</table>
$("#test").click(function() {
alert($(this).attr("id"))
alert($(this).closest("tr").attr("id"));
})
答案 1 :(得分:0)
首先在你的html代码中更改为:onClientClick = ValidateEmail($(this))
<asp:Button ID="btnEmailUpdate" Text = "Update" CommandName="UpdateEmail" runat="server" onClientClick="return ValidateEmail($(this))"/>
然后Jquery:
//the parameter will be the button object itself.
function ValidateEmail(btnObj)
{
alert(btnObj.closest("tr").attr('id'));
//replace the "txtName" with the id of the textbox whose value you need to validate.
alert(btnObj.closest("tr").find('input[id*="txtName"]').val());
alert(btnObj.prev()。val()); //将给出电子邮件地址文本框“txtgvEmailAddress”的值
返回false;
}
编辑:更多解释 '.id'属性适用于javascript对象,在你的代码中,btn_id是按钮的javascript对象(因为'this'不是用$(this)),所以btn_id.id可以工作。但$(btnId).closest(“tr”)将创建tr的jquery对象,而'.id'属性不可用,但您需要使用.attr('id')。 希望这能为你清除.. !!
编辑:添加代码以获取部分行的文本框值,获取电子邮件地址文本框值的代码
答案 2 :(得分:0)
要获取任何标记的值,请使用以下
$("selector").attr("property_name")
所以要获得以下效果
alert($(btnId).closest("tr").id);
使用
alert($(btnId).closest("tr").attr("id"));
以下是验证函数的jquery等效代码
$("#btn_id").click(function() {
//alert(btn_id.id);
alert($(this).attr("id"))
var btnId = $(this).attr("id");
//alert($(btnId).closest("tr").id);
alert($(this).closest("tr").attr("id"));
return false;
})
//include $('#<%=lblTest.ClientID%>').text(btn_id.id); accordingly
检查此fiddle
答案 3 :(得分:0)
我解决了这个问题。以下是包含答案的帖子的链接:
这是答案的链接:
How to get value of textbox inside of gridview
我的问题是我的GridView没有生成它的html,所以我不确定如何遍历DOM以获得我需要的值。在看到DOM之后,我能够将我的html发布到这个论坛并得到了正确的答案。