2014-11-03 57 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