有没有办法在使用 jQuery 单击它时获取超链接名称?我有以下代码,我需要一些 jQuery 指导:
<a href="#" id="imageClick" name='<%# Eval("fileName1") %>'><asp:Image ID="myImage" name='<%# Eval("fileName1") %>' runat="server" ImageUrl='<%#"~/Image/" + Eval("fileName") %>' /></a>
基本上,我想返回 <%# Eval("fileName1") %> 的值。
谢谢。
编辑:为了更清楚,我有一个弹出页面,其中包含一个带有图像和单选按钮的 listView。要求是如果您单击单选按钮,它应该获取该特定选项的值并关闭弹出窗口。我还将一个值传递回父窗口。所以这就是我所拥有的:
$(document).ready(function () {
$("#form1").change(function () {
var val = "";
if ($('input:radio[name=myRadio]:checked').val()) {
val = $('input:radio[name=myRadio]:checked').val();
}
if (val != "") {
$(window.opener.document).find('#txtLogos').val(val);
// Close the window
window.close();
}
});
});
当我单击其中一个单选按钮时,它工作正常。但现在他们增加了另一个要求,即如果他们点击图像,他们想要相同的结果(显然不会破坏单选按钮的功能)。
最佳答案
您只需在点击处理程序中使用 this.name
即可访问它。 this
这里是Dom元素(不需要jquery获取元素属性值),直接访问元素的name属性即可。
$('#imageClick').click(function(){
alert(this.name);
});
编辑
点击图片不会触发表单变化;不像 input, select, textarea 等。所以你需要在图像点击事件上手动触发表单更改(模拟单选按钮点击触发表单更改事件)。
将点击处理程序绑定(bind)到您的图像以添加类:
$('yourimageselector').click(function(){
$(this).toggleClass('checkedImage'); // Add a class on first click and again clicked on it remove the class to say it is turned off. If you dont want a turn off functionality simply say :
//$(this).addClass('checkedImage'); //or if this is the only class then this.className = 'checkedImage' classList is not yet supported in all browsers.
$('yourform').change(); //as this is image it wont trigger form change event so you need to manually trigger form's change event (only for input, select, textarea etc form change will be triggered).
});
在你的表单事件中:
$("#form1").change(function () {
var imgNames= $('.checkedImage')
.map(function(){return this.name; })
.get(); // Will get you all the image names in an array.
//if it is just one image then simply do if($('.checkedImage').length > 0) $('.checkedImage')[0].name,
//Code follows
});
Fiddle
关于javascript - 使用 jQuery 在单击时获取超链接的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241539/