您没有提供的“删除”链接,其中ID会被注意到的完整的上下文,所以我会假设的格式,你可以从那里适应。
<!-- HTML //-->
<!-- Format for Each Element. "123" is the ID here //-->
<div id="element123">
This is Element 123 (ID#123)
<a href="/php/removeSave.php?ID=123">Remove</a>
</div>
<!-- The Form at the Bottom of the Page //-->
<form id="removeSave" method="POST" action="/php/removeSave.php">
<input type="hidden" name="ID" value="" />
</form>
jQuery的段这种方法的
<script type="text/javascript">
$(document).ready(function(){
// Clicks of Links starting with "/php/removeSave.php?ID="
$('a[href^="/php/removeSave.php?ID="]').click(function(e){
// Don't let the Link act as a basic link
e.preventDefault();
// Get the Link HREF attribute
href = $(this).attr('href');
// Derive the Form ID and ID Value
bits = href.match(/^\/php\/([^\.]+)\.php\?ID=(\d+)/);
formID = bits[1];
elementID = bits[2];
// Set the Field Values and Submit the Form
$form = $('#'+formID);
$form.find('input[name="ID"]').val(elementID);
$form.submit();
});
});
</script>
好处?
优雅的降级 - 只要你的PHP脚本也可以处理GET变量,如果这个页面是从没有启用Javascruipt的浏览器加载的,或者无法加载jQuery,点击“删除”链接仍然会执行预期的操作。
机遇AJAX-ification - 而不是.click(function(e){
段内的所有这些其他操作,您可以使用jQuery的$.post()
功能和链接的HREF的查询字符串段直传球此请求处理程序和操作页面,而无需做一个完整的页面重新加载。
给出的链接似乎没有ID。此外,为了清楚起见,ID不能以数字开头。 – kojiro 2011-02-28 02:22:06
对不起,添加了链接ID。 – AKor 2011-02-28 02:30:31