2014-11-03 60 views
0

真的可以使用一些幫助/邏輯。即興 - 簡單傳遞變量到URL

我想通過一個jQuery的即興可以被用來追加到一個目標網址的href elementid。

即,先從A HREF:

<a class="link" href="javascript:;" elementid="66">Remove item</a> 

我希望它這樣,如果我點擊這個鏈接就會送我去:去除-element.php elementid = 66

我的JavaScript看起來像:

<script> 
function removeItem(id) { 
var txt = 'Are you sure you want to remove this item?'; 
$.prompt(txt, { 
buttons: {Yes: true, Cancel: false}, 
callback: function(accept) { 
if (accept) { 
window.location = "remove-element.php?elementid=x"; 
} 
return false; 
} 
}); 
} 

$('a.link').click(function() { 
removeItem($(this).data('id')); 
return false; 
}); 

</script> 

回答

1

爲了使用$(this).data('id'),你需要設置你的定位標記爲:

<a class="link" href="javascript:;" data-elementid="66">Remove item</a> 

和PAS s的值,如:

$('a.link').click(function() { 
    removeItem($(this).data('id')); 
    return false; 
}); 

function removeItem(id) { 
    var txt = 'Are you sure you want to remove this item?'; 
    $.prompt(txt, { 
     buttons: {Yes: true, Cancel: false}, 
     callback: function(accept) { 
      if (accept) { 
       window.location.href = "http://your_site.com/remove-element.php?elementid=" + id; 
      } 
      return false; 
     } 
    }); 
} 
+0

謝謝你一噸。真的很感謝幫助。乾杯。保羅 – Brandrally 2014-11-03 07:50:27