Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在使用JQuery选择类时获取单个atributes_Javascript_Jquery_Jsp_Attr - Fatal编程技术网

Javascript 在使用JQuery选择类时获取单个atributes

Javascript 在使用JQuery选择类时获取单个atributes,javascript,jquery,jsp,attr,Javascript,Jquery,Jsp,Attr,我不熟悉javascript和JQuery,我正在一个使用JSP的小项目中工作 我用JSP动态创建了一个网格,并添加了一些按钮,其中类为“select”,在alt属性中设置了当前行索引。这非常有效,我正在尝试动态设置onclick。这是我的密码 $('.select').click(function (){ alert($('.select').attr('alt')); } 我希望每个按钮都显示自己的索引,但代码只显示每个按钮中的第一个索引。我已经找遍了怎么做,但什么也没找到 有机会做

我不熟悉javascript和JQuery,我正在一个使用JSP的小项目中工作

我用JSP动态创建了一个网格,并添加了一些按钮,其中类为“select”,在alt属性中设置了当前行索引。这非常有效,我正在尝试动态设置onclick。这是我的密码

$('.select').click(function (){
   alert($('.select').attr('alt'));
}
我希望每个按钮都显示自己的索引,但代码只显示每个按钮中的第一个索引。我已经找遍了怎么做,但什么也没找到

有机会做我想做的吗?

将此行更改为:

alert($(this).attr('alt'));

当jQuery调用事件处理程序时,它会将
this
设置为有问题的DOM元素,因此请尝试以下操作:

$('.select').click(function (){
   alert($(this).attr('alt'));
});
如果需要访问DOM元素属性,则可以直接获取它们,例如:

alert( this.id );
this.value = "test";
如果需要对元素使用jQuery方法,则需要首先将其传递给jQuery函数,例如:

$(this).hide();
$(this).css("color","red").slideDown();
改变

alert($('.select').attr('alt')); 


现在,您可以选择启动事件按钮的attr alt。

不确定这是否是您要查找的内容,但是

$('.select').click(function() {
    $('.select').each(function() { 
        $(this).attr('value', $(this).attr('alt'));
    });
});
这将使每个按钮在单击一个按钮时“显示”其alt属性中存储的值


顺便说一句,如果您每行使用1个按钮,您可能最好使用。

您可以通过指定应替换的$('.select')使答案更清楚。
alert($(this).attr('alt')); 
$('.select').click(function() {
    $('.select').each(function() { 
        $(this).attr('value', $(this).attr('alt'));
    });
});