2013-05-22 36 views
0

我似乎無法弄清楚這一點。一旦你點擊了按鈕,它應該打開一個對話框來確認你是否想繼續「進入該鏈接」(即google.com)。如果是的話,它應該引導你到鏈接。然而,我找不到解決它的辦法。我有兩個不同鏈接的按鈕。Jquery UI對話框按鈕確認打開URL

視圖的jsfiddle here

HTML:

<button class="open" onclick="window.open('http://google.com')">Google</button> 
<button class="open" onclick="window.open('http://yahoo.com')">Yahoo</button> 

<div class="unique">Are you sure you want to continue?</div> 

JS:

$(function() { 
    $('.open').on("click", function(e) { 
     var link = this; 

     e.preventDefault(); 

     $('.unique').dialog({ 
      buttons: { 
       "Ok": function() { 
        window.location = link.href; 
       }, 
       "Cancel": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    }); 
}); 

CSS:

.unique {display: none;} 



但如果我使用以下(http://jsfiddle.net/mJwMu/) - 它工作正常。但是,我只能指向一個鏈接。事實並非如此 - 我希望能夠直接指向多個鏈接。 (google.com/yahoo.com/msn.com/etc)

HTML:

<button class="open">Google</button> 

<div class="unique">Are you sure you want to continue?</div> 

JS:

$(function() { 
    $('.open').on("click", function(e) { 
     var link = this; 

     e.preventDefault(); 

     $('.unique').dialog({ 
      buttons: { 
       "Ok": function() { 
        window.open('http://google.com'); 
       }, 
       "Cancel": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    }); 
}); 

CSS:

。獨特{display:none;}

感謝您的幫助!

+0

您正在嘗試使用href屬性('了window.location = link.href;'),其中不存在('<按鈕類=」打開「onclick =」window.open('http://google.com')「> Google')。 – j08691

回答

0

HTML

<button class="open" data-href="http://www.google.com">Google</button> 
<button class="open" data-href="http://www.yahoo.com">Yahoo</button> 
<div class="unique">Are you sure you want to continue?</div> 

jQuery的

$(function() { 
    $('.open').on("click", function (e) { 
     var link = this; 

     e.preventDefault(); 

     $('.unique').dialog({ 
      buttons: { 
       "Ok": function() { 
        window.open($(link).attr("data-href")); 
        $(this).dialog("close"); 
       }, 
       "Cancel": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    }); 
}); 
+0

這起作用。非常感謝! – user1652920

+0

不客氣,一定要點擊答案旁邊的複選框,讓每個人都知道你已經有了一個可行的答案。 –