php - 如何从调用 jQuery 函数的链接获取参数?

标签 php javascript jquery

我有一个如下所示的链接:

<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/

相关文章:

php - 从一个表中选择 Laravel 5.1 中另一个表中不存在的所有记录

javascript - 如何从其他 json 文件内部加载 json 文件?

javascript - 如何使用 DirectionsJS 更改 Mapbox 的标记?它显示 A 和 B 标记

php - 我的 Paypal IPN 脚本每次都被命中两次是否正常?

php - 提高 CURL 速度 php

php - 使用 Zend Framework 检查每个页面上的自定义条件

javascript - vue中如何声明变量而不返回它?

javascript - 自定义 AngularJS Bootstrap slider

javascript - 我希望在用户单击播放按钮时更改电台名称

jQuery:如何将类添加到已通过 ajax 插入到 DOM 中的元素