javascript - jQuery - 将变量传递给点击功能

标签 javascript jquery html

我有一个简单的 jQuery 问题,代码如下:

 <script>
    $(document).ready(function() {
    $("#changeText").click(function() {
        $.get("test.php", { sectid: "1"},
            function(data){
            $("#textBox").html(data);
        });
    });
    });
 </script>

我的HTML如下:

<a id="changeText" href="#">Change</a>
<div id="textBox">This text will be changed to something else</div>

我想将一个变量传递给 .click 函数来代替“1”,但似乎语法不正确。有人能给我指出正确的方向吗?

谢谢。

最佳答案

您只需使用变量名,例如:

$(document).ready(function() {
  $("#changeText").click(function() {
    var myVariable = "1";
    $.get("test.php", { sectid: myVariable },
        function(data){
        $("#textBox").html(data);
    });
  });
});

或者如果它是来自某物的值,例如:

$.get("test.php", { sectid: $(this).attr("something") },

关于javascript - jQuery - 将变量传递给点击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4549351/

相关文章:

javascript - 背景图像不会淡入

jQuery 动画性能

html - 使用顶部三 Angular 形向 div 添加边框

php - Facebook Like 按钮 - 如何获取 'liked' 某事的好友数量

javascript - jquery 使用其中的注释选择一个元素

jquery - Vue.js 在渲染组件前填充数据

html - 用于编辑 HTML 的 Chrome/Firefox 检查器

c# - 无法更改 javascript 中设置的 cookie 值?

javascript - 如何用单个字符替换 2 个连续字符

jquery - IE8 和 IE9 的 Fancybox 样式问题