2011-11-30 43 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]') –