2013-02-11 170 views
-1

我有一個html按鈕,應該點擊按鈕點擊javascript。它識別按鈕單擊並顯示確認警報,但不會繼續併發布console.logs或轉到ajax方法。 這裏是SCRIPT關閉:Javascript not firing on button click

 $('.Delete') 
     .click(function() { 
    return confirm("Are you sure you'd like to delete this entry?" 
     if(return = true) 
    { 
       var parent = $(this).parent().prev(); 
       var tr = $(this).closest('tr'); 


       var ValNode = tr.find(".typeText").first(); 
       var AddrNode = parent.children(".AddrText").first(); 

       var Val = ValNode.val(); 
       var addr = addrNode.val(); 

       console.log(Val); 
       console.log(addr); 

       var item = { Value: Val, Address: addr } 
       deleteItem(userID, item); 
    } 

     }); 

這是html元素,我認爲它可能與按鈕的元素它調用的關係做。我有他們作爲表親。

<asp:ListView runat="server" id="ListView1" > 
     <LayoutTemplate> 
       <thead> 
        <tr> 
         <th> 
          <a href="#">Type</a> 
         </th> 
         <th> 
          <a href="#">Address</a> 
         </th>                      
        </tr> 
       </thead> 
       <tbody> 
        <tr id="itemPlaceholder" runat="server" /> 
       </tbody> 
       <tfoot> 
       </tfoot> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr> 
       <td> 
        <input type="text" id="Type" class="TypeText " value="<%# Eval("Type")%>" />      
        <input type="text" id="Addr" class="AddrText " value="<%# Eval("Address")%>" />     
       </td> 
       <td> 
        <input type="button" id="btn_update" class="Update" value="Update" /> 
        <input type="button" id="btn_delete" class="Delete" value="Delete" /> 
       </td> 
      </tr> 
     </ItemTemplate> 
    </asp:ListView> 

回答

2

1. return是在JavaScript中保留字。它用於指定函數返回給調用者的內容。不要將它用於變量名稱。

變化

return confirm("Are you sure you'd like to delete this entry?"

var result = confirm("Are you sure you'd like to delete this entry?");

2.您if條件檢查是不正確

變化

if(return = true)

if(result == true)

1

return是保留關鍵字,返回執行到函數調用的當前功能。你需要的東西是這樣的:

var retVal = confirm("Are you sure you'd like to delete this entry?"); 
if (retVal) {...} 
1

你的問題在於return

$('.Delete') 
    .click(function() { 
     var sure = confirm("Are you sure you'd like to delete this entry?"); 
     if (sure) { 
      var parent = $(this).parent().prev(); 
      // ... 
      // the rest 
      // ... 
     } 
    }); 

return是在JavaScript中的保留字,所以你應該使用別的東西。

0

我會做類似

if (confirm(....)) 
{ 
} 

原因 「返回」 退出你的函數。