[英]alert when multiple elements with the same class name have an added class
[英]How to select an id through a class when you have multiple elements in the same page that have same class
我有这个数据表,其中我将表的行的ID分配给了一个链接,实际上每个链接都有一个唯一的ID。 我还为每个链接分配了一个类。
我想要实现的基本上是当我单击按钮时,可以通过在类中选择ID来将ID的值分配给文本框。
这是我分配的类和ID的链接。 对于表中的每个项目,在整个数据表中都会重复此操作。 我在控制器的ajax方法中运行此链接,这就是为什么id中有加号的原因,因为它是控制器中的字符串。
<a href='{0}' data-toggle='modal' data-target='#DivAppendToPartialView' id='"+item.mobileNumber+"' class='messageBtn'>
这是分配给文本框的功能
$(".messageBtn").click(function () {
var mobileNumberId = $(".messageBtn").attr("id");
$("#mobileTxtBox").val(mobileNumberId);
})
$(".messageBtn").click(function () {
var data = $(this).attr("id");
$("#mobileTxtBox").val(data);
})
尝试$(this)
,它选择当前的“ clicked”按钮。 如果使用$('.messageBtn)
保存到var中,则将所有元素保存在该类中。
$(".messageBtn").click(function () { var mobileNumberId = $(".messageBtn").attr("id"); console.log(mobileNumberId); $("#mobileTxtBox").val(mobileNumberId.substring(mobileNumberId.indexOf('+')+1, mobileNumberId.lastIndexOf('+'))); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a data-toggle='modal' data-target='#DivAppendToPartialView' id='"+item.mobileNumber+"' class='messageBtn'>click</a> <input type="text" id="mobileTxtBox">
通过删除ID中的+分配ID的确切值
对输入使用val()方法,对其他元素使用text()或html()方法。
$(".messageBtn").click(function () { var mobileNumberId = $(".messageBtn").attr("id"); $("#mobileTxtBox").val(mobileNumberId); $("#mobileP").text(mobileNumberId); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="mobileP"> </p> <p> <input id="mobileTxtBox" type="text"> </p> <a href='#' data-toggle='modal' data-target='#DivAppendToPartialView' id='"+item.mobileNumber+"' class='messageBtn'>Click ME</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.