最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery : Append jquery variable to a class name - Stack Overflow

programmeradmin5浏览0评论

My code

   $( ".attachPo" ).click(function() {
        alert($(this).attr('id'))   // prints 59
        var id = $(this).attr('id');
        $( '#attachPoForm_"+id+"').show();  // id name = attachPoForm_59
    });

but which does not work for me , what is the correct vay of appending jQuesy variable to a class or id name

My code

   $( ".attachPo" ).click(function() {
        alert($(this).attr('id'))   // prints 59
        var id = $(this).attr('id');
        $( '#attachPoForm_"+id+"').show();  // id name = attachPoForm_59
    });

but which does not work for me , what is the correct vay of appending jQuesy variable to a class or id name

Share asked Oct 15, 2013 at 12:11 mondamonda 3,91516 gold badges61 silver badges85 bronze badges 1
  • 1 $('#attachPoForm_'+id) – mikakun Commented Oct 15, 2013 at 12:13
Add a ment  | 

6 Answers 6

Reset to default 3

Your quotes aren't quite right. Change to:

$( '#attachPoForm_'+id).show();

This isn't a "jQuery variable", it's just simple Javascript variable and string concatenation.

Your selector is looking for an element with an id of (literally) #attachPoForm_"+id+", I think you mean:

$( '#attachPoForm_'+id).show();

Try this

$( ".attachPo" ).click(function() {
    var id = this.id
    $( '#attachPoForm_'+id).show();  // id name = attachPoForm_59
    //                ^^^^ Fixed the quotes here 
});
 $( ".attachPo" ).click(function() {
       $( '#attachPoForm_'+ $(this).attr('id')).show();  //set  id name = attachPoForm_59
    });

A mistake at

    $( '#attachPoForm_"+id+"').show();  // id name = attachPoForm_59

should be

    $( '#attachPoForm_'+id+'').show();  // id name = attachPoForm_59

Try to use this.id like,

$( ".attachPo" ).click(function() {
    var id=this.id;
    alert(id);
    $("#attachPoForm_"+id).show();  // id name = attachPoForm_59
});
发布评论

评论列表(0)

  1. 暂无评论