javascript - 我的简单 jquery 验证不起作用

标签 javascript jquery html validation

嘿伙计们,这是我的代码:

 <script>$('td#view-details').click(function() {
     var fg = $(this).closest('td').siblings(':first-child').text();     
     $('#'+fg+'confirm').click(function () {
         if($('form #' + fg + 'input[name=test]').val() == "") {
             $('#error_notification').fadeIn(100).delay(1000).fadeOut(100);
             return false;
         } else {
             $(main).text('show');
             $('#' + fg).addClass('details').removeClass('active');
             $('#success_notification').fadeIn(100).delay(1000).fadeOut(100);
         }
     });
</script>
<html>
    <table width="742" id="redemptions_landing_page" valign="top">
        <tr>
            <th>ID</th>
            <th>Investor Name</th>
            <th>Email</th>
            <th>Credit</th>
            <th>Request Amount</th>
            <th>Request Mode</th>
            <th>View</th>
        </tr>
        <tr>
            <td>1848719408</td>
            <td>arvind</td>
            <td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93ebebebeaead3f4fef2faffbdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a></td>
            <td>Rs 5000</td>
            <td>Rs 300</td>
            <td>Cheque</td>
            <td id="view-details">show</td>
        </tr>
    </table>
    <form  id="1848719408">
        <textarea cols="40" rows="10" id="remarks" placeholder="Remarks (enter courier number and courier service provider for confirmed redemptions, enter reason for rejected redemptions)"></textarea>
    <h3 class="bbrdt">Redemption Amount</h3>
    <input type="text" name="test" id="Amount_For_Investor" placeholder="Amount For Investor" />&nbsp;&nbsp;&nbsp;<input name="test" type="text" id="courier_charges" placeholder="Courier Charges"/>&nbsp;&nbsp;&nbsp;
<input value="Confirm" type="button" id="1848719408confirm" />
<button id="reject">Reject</button></form>

我想验证文本字段,但它不起作用,当文本字段为空时,会再次显示成功通知,但不会显示错误通知。

最佳答案

您应该始终将代码包装在 .ready 中确保 DOM 已加载的函数,如下所示

$(document).ready(function(){
    $('td#view-details').click(function() {
    var fg = $(this).closest('td').siblings(':first-child').text();
    $('#' + fg + 'confirm').click(function() {
        if ($('form #' + fg + 'input[name=test]').val() == "") {
            $('#error_notification').fadeIn(100).delay(1000).fadeOut(100);
            return false;
        }
        else {
            $(main).text('show');
            $('#' + fg).addClass('details').removeClass('active');
            $('#success_notification').fadeIn(100).delay(1000).fadeOut(100);
        }
    });
});

关于javascript - 我的简单 jquery 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322854/

相关文章:

javascript - Bootstrap Accordion 不能与里面的 table 一起工作

javascript - 如何在重定向后显示另一个页面的隐藏div

html - 自举行四 block

javascript - 时间选择器单击一次不显示时间

javascript - 单击更改 Div 类

javascript - Jquery,停止排队函数并重置为原始函数

javascript - 保持背景固定但让照片滚动

javascript - Lambda 错误未被 try/catch block 或错误处理程序捕获

javascript - 如何防止两个react-native Picker相互影响?

JavaScript - 找出一个数字均匀地进入另一个数字的次数