我的简单jquery验证不起作用

时间:2011-11-30 08:10:41

标签: 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>xxxyy@gmail.com</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>

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

4 个答案:

答案 0 :(得分:1)

您错过了准备好的文件。在您绑定click事件时,该元素尚不存在。

试试这个

<script>
    $(function(){
    // Your code go's here
    });
</script>

答案 1 :(得分:1)

正如Niels指出的那样,你应该在开始之前等待文档准备就绪,因为你正试图访问一些尚未创建的东西。

你应该试试这个:

<script>
   $(document).ready(function(){
     // Your code
   });
</script>

答案 2 :(得分:1)

您应始终将代码包装在.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);
        }
    });
});

答案 3 :(得分:0)

虽然我同意将文件准备好包装是好的做法,但我认为这不是你的问题。

<script>
$('td#view-details').click(function() {
var fg = $(this).parent().find(":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>

var fg = $(this).closest('td')。siblings(':first-child')。text(); //此行已更改为 var fg = $(this).parent()。find(“:first-child”)。text();

$('#'+ fg +'确认')。click(function(){//此行缺少确认空格

if($('form#'+ fg +'input [name = test]')。val()==“”){//此行缺少输入空间

希望这有帮助!