2012-12-03 125 views
0

我有这样的代码的jQuery与关闭按钮

<input type='submit' name='sub_form' value='Save'><a href="#" class="close">Cancel</a>

和jQuery是

$('td.add_text').click(function(e) { 
    $('<img src="images/ajaxLoader.gif" id="loading" style="width:auto;" />').appendTo(".quotation_computation"); 
    //Cancel the link behavior 
    e.preventDefault(); 
    //Get the A tag 
    var ccom = $('input[name=com_code]').val(); 
    var ucom = $('input[name=user_code]').val(); 
    var in_form = $('input[name=in_form]').val(); 
    var ctrack = $('input[name=com_for_track]').val(); 

    $.ajax({ 
     type:"GET", 
     url:"forms/tem.php", 
     data: {uc: ucom, cc: ccom, ct: ctrack, in_f: in_form }, 
     dataType: 'html', 
     target: '.quotation_computation', 
     success: function(data){ 
      $(".quotation_computation").find('img#loading').remove(); 
      $(".quotation_computation").html(data); 
     } 
    }) 
}); 

和关闭按钮的jQuery

$(".close").click(function() { 
     $(".quotation_computation").hide(); 
}); 

和HTML

<div class="quotation_computation"> 
ANOTHER JQUERY PROBLEM 
</div> 

当我点击关闭按钮它不工作。

是我的关闭按钮有误吗? okie当我点击div class="quotation_computation"> jquery ajax工作正常,它保存到我的数据库。但是当我点击Cancel按钮时,jquery ajax或表单仍然存在。

有什么我需要改变?

+4

你是否执行你的代码来在'$(document).ready'中附加click事件? –

+0

什么是$ .ajax()的“目标”选项?我无法在API文档中找到它 – devnull69

+0

单击关闭链接时是否收到任何javascript错误? – Novocaine

回答

1

单击<a href="#">定位标记将具有默认操作,即它将强制页面刷新。您必须防止此默认操作,否则会刷新页面并重新打开您刚刚隐藏的元素。

$('.close').click(function(e) { 
    $('.quotation_computation').hide(); 
    e.preventDefault(); 
}); 
+2

'href =“#”'不刷新页面;它只是滚动到页面的顶部。但是,是的,你应该总是使用'e.preventDefault()'。 –

+0

它刷新Firefox中的页面:http://jsbin.com/idajaz/1/edit – devnull69

+0

@ devnull69奇怪,这可能是一个jsbin的bug ...不是? – nicolast