2016-11-02 52 views
0

這裏是我的代碼:如何獲得點擊元素的href值?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<script src="https://cdn.rawgit.com/makeusabrew/bootbox/master/bootbox.js"></script> 
 

 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 

 
<a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location = this.href;} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">Link</a>

注意到我使用this libraryconfirm

,但此行並不在我的代碼工作:

window.location = this.href; 

,它總是(當我點擊OK重定向我在這裏:

http://localhost:8000/undefined 

如何解決它?

+0

$ tb未定義。你使用任何模板引擎或框架? –

+0

難道你不使用e.target代替這個嗎? – Aer0

+0

你可以做一個js小提琴嗎? @stack –

回答

2
<a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location.href = $(this).attr('href');} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete"> 

而不是onClick你可以做這樣的事情。代替了window.location的

<a id="some-id" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete"> 
$('#some-id').click(function(){ 
    var link = $(this).attr('href'); 
    bootbox.confirm('are you sure ?', function(e){ 
     if(e){ 
     window.location.href = link; 
     } 
    }) 
}) 
+0

它仍然會將我重定向到'undefined'頁面 – stack

+0

您能創建一個簡單的小提琴嗎?或者給我一點時間,我會爲你創造它。 – imrealashu

+0

好吧,我從功能(e,鏈接){,'刪除'鏈接',現在它也可以,謝謝 – stack

-3

是在this.ref範圍內的函數? 兩年前寫JS的我最後的行...

編輯: 我記得,問題是返回值。如果它不是flase。它將移動到href位置。

沒有你libary,但你能適應它:

<a onclick="if(confirm('are you sure?')) { alert(this.href);};return false;" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">Click me</a>

編輯2: 愚蠢的反對投票之前,你應該嘗試的代碼。有用。

-1

使用window.location.assign,它可以爲你想要的工作或window.location.href會工作。

0

使用此代碼它會給你一個彈出,如果你點擊是,那麼它會進一步處理,否則留在同一頁面上。 試試這個。

<a onclick="return confirm('Are you sure you want to delete this?');" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete"> 
0

Bootbox是第三方插件。您無法在bootbox confirm回調函數內訪問此指針。在使用bootbox confirm回調中的局部變量之後,必須首先在本地變量中保存this.href,然後再調用bootbox。

<a onclick="NavigatePage()" href="/Remove-Post/{{ $tb }}/{{$post->id}}"/> 

function NavigatePage(){ 
    var URL = this.href; 
    bootbox.confirm('are you sure?', function(UserResponse){ 
    if (UserResponse) { 
     window.location = URL 
    } 
    }); 
} 

在你的情況,因爲你正在訪問的bootbox回調函數內this對象,你會得到不確定的。回撥內部this對象無法訪問。

1

在你的代碼中this.href不會因爲this工作是指的在bootbox對象,這最後沒有一個href屬性:

我建議你只是使用一個共同的功能是用於顯示對話框,並以同樣的方式,你可以使用幾個重定向到一個網址。也並設置動態地在對話框的標題是傳遞它作爲一個參數,

這裏是一個片段: (我已經conosle.log防止重定向更換location.href ...)

function redirect(node,message) { 
 
    if(!message) message =""; 
 
    return bootbox.confirm(message, function(e){ if (e) { 
 
    // we use console instead of redirecting directly 
 
    console.log(node.href); 
 
    // location.href = node.href; 
 
    } }); 
 
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> 
 
<script src="http://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script> 
 

 

 

 
<a onclick="redirect(this,'are you sure ?');" href="http://www.google.com" data-toggle="modal" data-target="#confirm-delete">link 1</a> 
 
<br> 
 
<a onclick="redirect(this,'leave the page ?');" href="http://www.bing.com" data-toggle="modal" data-target="#confirm-delete">link 2</a> 
 
<br> 
 
<a onclick="redirect(this,'are you sure to leave !! ?');" href="http://www.yahoo.com" data-toggle="modal" data-target="#confirm-delete">link 1</a>

+0

@stack你可以使用一個功能和優化的解決方案 –

相關問題