1

我的代码

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

但这对我不起作用,将 jQuesy 变量附加到类或 id 名称的正确方法是什么

4

6 回答 6

3

你的报价不太正确。改成:

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

这不是“jQuery 变量”,它只是简单的 Javascript 变量和字符串连接。

于 2013-10-15T12:13:41.060 回答
1

您的选择器正在寻找一个 id 为 (literally) 的元素#attachPoForm_"+id+",我认为您的意思是:

$( '#attachPoForm_'+id).show();
于 2013-10-15T12:13:33.670 回答
1

试试这个

$( ".attachPo" ).click(function() {
    var id = this.id
    $( '#attachPoForm_'+id).show();  // id name = attachPoForm_59
    //                ^^^^ Fixed the quotes here 
});
于 2013-10-15T12:13:49.450 回答
0
 $( ".attachPo" ).click(function() {
       $( '#attachPoForm_'+ $(this).attr('id')).show();  //set  id name = attachPoForm_59
    });
于 2013-10-15T12:13:37.933 回答
0

一个错误在

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

应该

    $( '#attachPoForm_'+id+'').show();  // id name = attachPoForm_59
于 2013-10-15T12:13:45.697 回答
0

尝试使用this.id 之类的,

$( ".attachPo" ).click(function() {
    var id=this.id;
    alert(id);
    $("#attachPoForm_"+id).show();  // id name = attachPoForm_59
});
于 2013-10-15T12:15:40.933 回答