jQuery确认链接点击

时间:2014-02-05 15:16:47

标签: jquery html

我正在尝试确保用户想要从数据库中删除条目。我宁愿暂停初始函数中的操作而不是尝试在删除函数中执行它,因为这更有意义。无论如何,该网站不断跳过确认对话框,只是继续删除该条目。我不确定它是否只是一个语法错误,但我无法弄清楚为什么我的jQuery不会跳闸。

<script type="text/javascript" src="jquery-1.10.2.min.js" ></script>
<script>
$(document).ready(function(){
    ('a.confirm').click( function( event){
        event.preventDefault();
        var url = $(this).attr('href') ;
        var confirm_box = confirm( "Are you sure you want to delete this player?" );
        if( confirm_box ){
            window.location = url;
        }
    } );
});
</script>
<?php 
function roster_table()
{
    try{
        if( isset( $_SESSION['session_id'] )  )
        {
            $db = honneyconnect( ) ;  // db connection established
            if( mysqli_connect_error() )
            {
            throw new Exception( "Could not connect to the database") ;
        }
        $query = 'select * from roster' ;
        $players = $db->query( $query ) ;
        if( !$players  )
        {
            throw new Exception ( "Query returned zero results" ) ;
        }
        else
        {
            $number_of_players = $players->num_rows ;
            echo "<div id='player_table'>
            <table><th>Hells Canyon Honeys Active Skaters</th>" ;
            for( $i = 0; $i < $number_of_players; $i++ )
            {
                $row = $players->fetch_row() ;
                echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td>
                <td><a href='http://localhost/honeysproject/editplayer.php?player_id=".$row[0]."'>Edit</a></td>
                <td><a class='confirm' href='http://localhost/honeysproject/deleteplayer.php?player_id=".$row[0]."'>Delete</a></td></tr>" ;
            }
            echo "<tr><td><a href='http://localhost/honeysproject/playerinfoentry.php'>Add Player</a></td></tr></table></div>" ;
        }
    }
    else
    {
        throw new Exception( "You are no longer authorized to view this document.  Please re-authenticate!<br>" ) ;
        session_destroy( ) ;
    }
    }
    catch( Exception $error ) 
    {
        echo $error ;
    }

}

3 个答案:

答案 0 :(得分:1)

更改('a.confirm').click( function( event){

$('a.confirm').click( function( event){

答案 1 :(得分:1)

通过$选择元素时,您错过了(a.confirm).click(...)

修复如下:

$('a.confirm').click(function(event){
    //...
});

此外,您只需使用以下内容即可简化整个脚本:

$(document).ready(function(){
   $('a.confirm').click(function(event){
         return confirm( "Are you sure you want to delete this player?" );
   });
});

根据返回值(truefalse),重定向将自动处理。

答案 2 :(得分:-1)

你在选择器('a.confirm')中缺少$。 关于确认对话this可能应该帮助你