2011-11-30 29 views
2

嘿这里家伙简单jQuery验证是我的代码:我不工作

<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>[email protected]</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> 

我要验证的文本字段,但它不工作,成功通知再次表示当文本字段为空,但错误通知不显示。

+0

也许这是一个错字,但你有一个非常错误的'HTML '标记它应该是'body'还是'div' – Luc125

+0

我已经在本地添加了文档准备功能,我忘记了在这里忘记 –

回答

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); 
     } 
    }); 
}); 
1

您缺少准备好的文档。目前,您正在绑定事件click,该元素尚不存在。

试试这个

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

正如尼尔斯指出,应该等待该文档是什么开始前准备好,因为你试图访问尚未创建的一些事情。

而应该试试这个:

<script> 
    $(document).ready(function(){ 
    // Your code 
    }); 
</script> 
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> 

变种FG = $(本).closest( 'TD')兄弟姐妹( ':第一胎')文字(); // this line changed to var fg = $(this).parent()。find(“:first-child”)。text();

$( '#' + FG + '确认')。点击(函数(){//此行失踪了确认

如果($( '#形式' + FG +“输入一个空格[名称=测试]')。VAL()==‘’){//此行缺少空间输入

希望这有助于!

+0

没有那些空格你的选择器会看起来像: –

+0

$('#1848719408confirm')w这是没有意义的 –

+0

$('form#1848719408input [name = test]') –