javascript - jQuery - Pass variable to click function - Stack Overflow

I have a simple jQuery question with the following code:<script>$(document).ready(function() {$(&

I have a simple jQuery question with the following code:

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

My HTML is as follows:

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

I'd like to pass a variable into the .click function in place of "1" but can't seem to get the syntax correct. Can someone point me the right direction?

Thanks.

I have a simple jQuery question with the following code:

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

My HTML is as follows:

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

I'd like to pass a variable into the .click function in place of "1" but can't seem to get the syntax correct. Can someone point me the right direction?

Thanks.

Share Improve this question edited Dec 28, 2010 at 20:43 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Dec 28, 2010 at 20:40 SethSeth 1234 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You just use the variable name, for example:

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

Or if it's a value from something for example:

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

You can set the variable before the function is called. Then read it's value after.

Example (using jQuery's .data):

$("#changeText").data('sectid', '1');

 $("#changeText").click(function() {
    $.get("test.php", {sectid: $(this).data('sectid')},
        function(data){
          $("#textBox").html(data);
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745317886a4622302.html

相关推荐

  • javascript - jQuery - Pass variable to click function - Stack Overflow

    I have a simple jQuery question with the following code:<script>$(document).ready(function() {$(&

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信