简体   繁体   English

jQuery:将jquery变量附加到类名

[英]Jquery : Append jquery variable to a class 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 但是这对我不起作用,将jQuesy变量附加到类或ID名称的正确方法是什么?

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. 这不是“ jQuery变量”,只是简单的Javascript变量和字符串连接。

您的选择器正在寻找ID为(字面意义上) #attachPoForm_"+id+"的元素,我认为您的意思是:

$( '#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, 尝试像这样使用this.id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM