有没有办法在使用jQuery点击它时获取超链接名称?我有以下代码,我需要一些jQuery方向:
<a href="#" id="imageClick" name='<%# Eval("fileName1") %>'><asp:Image ID="myImage" name='<%# Eval("fileName1") %>' runat="server" ImageUrl='<%#"~/Image/" + Eval("fileName") %>' /></a>
基本上我想返回&lt;%#Eval(“fileName1”)%&gt; 的值。
感谢。
编辑:为了更清楚,我有一个弹出页面,其中包含一个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();
}
});
});
单击其中一个单选按钮时,此功能正常。但是现在他们又增加了一个要求,即如果他们点击图像他们想要相同的结果(显然不会破坏单选按钮的功能)。
答案 0 :(得分:4)
您只需使用点击处理程序中的this.name
即可访问它。 this
这里是Dom元素(不需要jquery来检索元素属性值),所以只需直接访问元素的name属性。
$('#imageClick').click(function(){
alert(this.name);
});
如果单击图像,则不会触发表单更改;与输入,选择,textarea等不同。因此,您需要在图像点击事件上手动触发表单更改(模拟单选按钮单击以触发表单更改事件)。
将点击处理程序绑定到您的图片以添加类:
$('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
});
答案 1 :(得分:2)
这也适用于点击的事件处理程序:
document.getElementById("new-answer-activity").name