jquery
  • dialog
  • click
  • href
  • 2011-07-04 23 views 1 likes 
    1

    我有這樣的jQuery代碼來確認鍵:jQuery的,用戶從點擊網址,並通過在對話框

    $("#deletec-box").dialog({ 
        autoOpen: false, 
        resizable: false, 
        height:230, 
        modal: true, 
        buttons: { 
         "Confirm": function() { 
          window.location = 'hrefurlfromclick'; 
          $(this).dialog("close"); 
         }, 
         Cancel: function() { 
          $(this).dialog("close"); 
         } 
        } 
        }); 
    
        $("#deletec-confirm").click(function() { 
         $("#deletec-box").dialog("open"); 
         return false; 
        }); 
    

    然後在頁面上我有一個調用對話框的鏈接:

    <a href="?action=delc&cid=2" id="deletec-confirm">Delete</a> 
    

    我問題是我如何獲得href的價值,所以如果人確認它加載他們最初點擊的鏈接?

    +0

    得到它嘗試這種解決方案:http://stackoverflow.com/a/14899441/1500341 我用它得到了成功。 – 2015-06-09 17:04:44

    回答

    3

    你可以得到href屬性:$('#deletec-confirm').attr('href');

    您的代碼現在看起來像:

    $("#deletec-box").dialog({ 
        autoOpen: false, 
        resizable: false, 
        height:230, 
        modal: true, 
        buttons: { 
         "Confirm": function() { 
          window.location = $('#deletec-confirm').attr('href'); 
          $(this).dialog("close"); 
         }, 
         Cancel: function() { 
          $(this).dialog("close"); 
         } 
        } 
    }); 
    
    $("#deletec-confirm").click(function() { 
        $("#deletec-box").dialog("open"); 
        return false; 
    }); 
    
    0

    你有使用jQuery UI的對話框?你也可以使用的確認框,如下所示:

    $("#deletec-confirm").click(function(event) { 
    
        if(!confirm("Are you sure you want to delete this item?")) { 
         event.preventDefault(); 
        } 
    
    }); 
    

    這樣,如果取消按鈕被擊中防止原訴訟中的鏈接,否則鏈接可以自由地執行它的原始作用。

    Here's a quick example.

    0

    使用attr

    var url = $('#deletec-confirm').attr('href'); 
    $("#deletec-box").dialog({ 
    autoOpen: false, 
    resizable: false, 
    height:230, 
    modal: true, 
    buttons: { 
        "Confirm": function() { 
         window.location = url; 
         $(this).dialog("close"); 
        }, 
        Cancel: function() { 
         $(this).dialog("close"); 
        } 
    } 
    }); 
    
    相關問題