为什么没有实现此功能? [英] Why isn't this function ever being reached?

查看:139
本文介绍了为什么没有实现此功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有使用带有淡出功能的功能?每次该函数运行时,它都会刷新页面,而不是使用AJAX.

Why isn't the function with fadeOut ever being reached? Every time the function runs it refreshes the page instead of using AJAX.

<script type="text/javascript">


function deleterow(id){
if (confirm('Are you sure want to delete?')) {
$.post('delete.php', {id: +id, ajax: 'true' },
function(){
$("#row_"+id).fadeOut("slow");
});
}
}

</script>

这是php输出的html:

Here is the html output by php:

<button onclick="deleterow('.$entry['rowid'].')" class="fg-button fg-button-icon-left ui-state-default ui-corner-all"><span class="ui-icon ui-icon-trash"></span></button>

推荐答案

很难确定地说没有看到调用该函数的代码/html,但是我认为它以某种方式附加到了onclick处理程序上.因此,如果您不阻止默认操作(在a标记的href之后),它将刷新页面.您需要从处理函数中返回false或从中调用event.preventDefault().

Its hard to say for certain without seeing the code/html that invokes the function but I assume this is attached to an onclick handler somehow. Therefore if you do not prevent the default action (following the href of the a tag) its going to refresh the page. You need to return false from the handler function or callevent.preventDefault() from it.

我将改用此标记:

<button 
  value="$entry['rowid']"
  class="fg-delete fg-button fg-button-icon-left ui-state-default ui-corner-all">
  <span class="ui-icon ui-icon-trash"></span>
</button>

通过在dom中对rowid进行编码(在本例中为value),我们可以在函数中对其进行访问,而不必将其硬编码到函数调用中并保持处理程序不引人注目.

By encoding the rowid in the dom (in this case as value) we can access it in our functions without having to hard code it into the function call and keep the handler unobtrusive.

// pulling this into a full definition for clarity
// you could of course just use an anon function inside you $.click call
function handleDelete(event){
   event.preventDefault();
   var $this = $(this);
   var rowId = $this.val();
   deleterow(rowId);
   return false;
}


$('button.fg-delete').click(handleDelete);

演示: http://jsfiddle.net/eBa7v/1/

这篇关于为什么没有实现此功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆