繁体   English   中英

如何使用jQuery获取TD的InnerHTML并在按钮单击上添加文本

[英]How to get innerhtml of td and add text on button click using jquery

我需要在写行名的td旁边添加*,例如rate。 如果发生错误,那么我需要在相关行旁边添加*。 单击按钮进行验证。

有人帮助我编写了代码,但是我找不到如何将*附加到td的答案。 因此,如果任何行中都有错误,我需要在相关行旁边添加*,但在td中添加诸如yield *之类的文本

谢谢

  <script type="text/javascript"> $(function () { $('#<%=form1.ClientID%>').submit(function (event) { //iterate over the rows $('.customValidationFrom').each(function (idx, obj) { //get the input objects and values var from = $(obj); var to = $('#' + $(obj).attr("ID") + "To"); var fromVal = parseInt(from.val()); var toVal = parseInt(to.val()); //Cancel the submit event.preventDefault(); //check range if (fromVal > 100 || toVal > 100) { var errorMessage = "<li id='" + from.attr('ID') + "Error'>The " + from.attr('data-name') + " field must be less than 100.</li>"; //Cancel the submit event.preventDefault(); $('#errorMessageList').append(errorMessage); } //Validate if both from and to inputs have values if (from.val() && to.val()) { if (fromVal > toVal) { //display error from.addClass('errorBorder'); to.addClass('errorBorder'); var errorMessage = "<li id='" + from.attr('ID') + "Error'>The " + from.attr('data-name') + " field must be less than the " + to.attr('data-name') + " field.</li>"; //Cancel the submit event.preventDefault(); } else { //reset error $('#errorMessageList li').remove(); from.removeClass('errorBorder'); to.removeClass('errorBorder'); } $('#errorMessageList').append(errorMessage); } }); }); }); </script> </head> <body> <form id="form1" runat="server"> <ul id="errorMessageList" class="errorText"> </ul> <div> <table id="tbl1"> <tr> <td class="style1"> rate </td> <td > <asp:TextBox ID="txtRate" runat="server" class="customValidationFrom" data-name="rate from"></asp:TextBox> </td> <td > <asp:TextBox ID="txtRateTo" runat="server" class="customValidationTo" data-name="rate to"></asp:TextBox> </td> </tr> <tr> <td class="style1"> test</td> <td> <asp:TextBox ID="txtYield" runat="server" class="customValidationFrom" data-name="yield from"></asp:TextBox> </td> <td > <asp:TextBox ID="txtYieldTo" runat="server" class="customValidationTo" data-name="yield from"></asp:TextBox></td> </tr> <tr> <td class="style1"> yeld </td> <td> <asp:TextBox ID="txtCal" runat="server" class="customValidationFrom" data-name="cal from"></asp:TextBox></td> <td "> <asp:TextBox ID="txtCalTo" runat="server" class="customValidationTo" data-name="cal to"></asp:TextBox></td> </tr> </table> <asp:Button ID="Button1" runat="server" Text="Button" /> 

尝试:

$('td').each(function() { 
    var text = $(this).text();
    if ( text == 'rate') {
        $(this).append('*')
    }
});

JSFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM