2010-03-23 69 views
0

對JQuery來說,我添加了下面的JQuery代碼,並將其移到我的代碼中,現在它不起作用我忘了我做了什麼,有人可以通過將下面的代碼放在正確的位置來修復我的代碼。JQuery代碼問題?

$('a').click(function() { 
    $('#changes-saved').remove(); 
}); 
    return false; // prevent normal submit 
}); 

JQuery代碼。

$(function() { 
    $('#changes-saved').hide(); 
    $('.save-button').click(function() { 
     $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) { 
      $('div.contact-info-form').html(html); 
      $('#changes-saved').append('Changes saved!').show().pause(1000).hide(); 
     }); 
     return false; // prevent normal submit 
    }); 

    $('a').click(function() { 
     $('#changes-saved').remove(); 
    }); 
     return false; // prevent normal submit 
    }); 
}); 

回答

0

防止<a>在瀏覽器中傳播。

$('a').click(function (event) { 
    if (!event) event = window.event; 
    if (event.preventDefault) 
     event.preventDefault(); 
    else 
     event.returnValue = false; 


    $('#changes-saved').remove(); 

    //In case the event propagation didn't work....(mostly dumb IE) 
    return false; // prevent normal submit 
}); 

從您的代碼,剛修好

$(function() { 
    $('#changes-saved').hide(); 
    $('.save-button').click(function() { 
     $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) { 
      $('div.contact-info-form').html(html); 
      $('#changes-saved').append('Changes saved!').show().pause(1000).hide(); 
     }); 
     return false; // prevent normal submit 
    }); 
    $('a').click(function() { 
     $('#changes-saved').remove(); 
     return false; // prevent normal submit 
    }); 
    }); 
}); 
1

它看起來像一個額外粘貼導致的超額收益和關閉塊,只是在結尾處,刪除這樣的:

return false; // prevent normal submit 
}); 
0

這是你想要的嗎?如果在末尾有額外的)};,並且return false;不在單擊事件處理函數中。

$(function() { 
    $('#changes-saved').hide(); 

    $('.save-button').click(function() { 
     $.post(
      $('#contact-form').attr('action'), 
      $('#contact-form').serialize(), 
      function(html) { 
       $('div.contact-info-form').html(html); 
       $('#changes-saved').append('Changes saved!') 
        .show().pause(1000).hide(); 
      } 
     ); 

     return false; // prevent normal submit 
    }); 

    $('a').click(function() { 
     $('#changes-saved').remove(); 

     return false; // prevent normal submit 
    }); 
});