我有一个如下所示的链接:
<p class="half_text">
<?php echo $upvotes; ?>
<strong><a class="vote_up" style="color: #295B7B; font-weight:bold;" href="#">Vote Up</a></strong> |
<?php echo $downvotes; ?>
<strong><a class="vote_down" style="color: #295B7B; font-weight:bold;" href="#">Vote Down</a></strong>
</p>
我的 jQuery 代码如下所示:
<script type="text/javascript">
$(document).ready(function()
{
$('.vote_up').click(function()
{
alert("up");
alert ( "test: " + $(this).attr("problem_id") );
// $(this).attr("data-problemID").
$.ajax({
type: "POST",
url: "/problems/vote.php",
dataType: "json",
data: dataString,
success: function(json)
{
// ? :)
}
});
//Return false to prevent page navigation
return false;
});
$('.vote_down').click(function()
{
alert("down");
//Return false to prevent page navigation
return false;
});
});
</script>
如何获取 Problem_id 参数值?如果我在 href 参数中添加 url,我认为浏览器只会转到该 url,不是吗?否则 - 如何将参数值打包到 jQuery 中?
谢谢!
最佳答案
因为你的$.ajax
是在变量的同一作用域中定义的,所以你可以使用problem_id
来获取变量值。
当前代码的概述:
var problem_id = "something"; //Defining problem_id
...
$.ajax(
...
success: function(){
...
//problem_id can also be accessed from here, because it has previously been
// defined in the same scope
...
}, ...)
....
关于php - 如何从调用 jQuery 函数的链接获取参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7665033/