用经典的ASP编写,循环生成一个按钮列表,每个按钮都有一个唯一ID “ myButton001”,“ myButton002”等。单击按钮时,我在 jQuery 中捕获 id 并将其保存到input type=hidden.
之后,我想获取隐藏的 button ID 并使用它更改按钮文本(html)来表达其他内容。
// Get the button that opens the modal
$(".rsvpChange").click(function() {
var thisMyBtnId = this.id;
$("#hdnMyBtnId").val(thisMyBtnId);
var modal = document.getElementById("myModal");
modal.style.display = "block";
});
// In the modal I have a btnRSVPChange that calls an Ajax process.
// on return from Ajax I want to change the button from hdnMyBtnId.
var buttonId = $("#hdnMyBtnId").val();
$(buttonId).html(rsvpChange); // this does not work.
因此,我正在寻找一种检索按钮ID并更改html按钮文本的方法。
答案 0 :(得分:2)
在JQuery中,通过在ID前面加上井号(例如#MyID)来按ID查找元素,因此请更改为以下内容:
var buttonId = $("#hdnMyBtnId").val();
$("#" + buttonId).html(rsvpChange); // this might work.
答案 1 :(得分:0)
因为Jquery的所有查询选择器在 Id
之前需要“#”尝试一下
var buttonId = "#"+$("#hdnMyBtnId").val();
$(buttonId).html(rsvpChange);
答案 2 :(得分:0)
尝试一下,看看是否有帮助
var buttonId = $("#hdnMyBtnId").val();
$(document).find(`[id=${buttonId}]`).html(rsvpChange); //This will work :)
答案 3 :(得分:0)
您需要更新最后一行
// Get the button that opens the modal
$(".rsvpChange").click(function() {
var thisMyBtnId = this.id;
$("#hdnMyBtnId").val(thisMyBtnId);
var modal = document.getElementById("myModal");
modal.style.display = "block";
});
// In the modal I have a btnRSVPChange that calls an Ajax process.
// on return from Ajax I want to change the button from hdnMyBtnId.
var buttonId = $("#hdnMyBtnId").val();
$("#"+buttonId).html(rsvpChange); // You need to update this