JS动态添加元素后事件不起作用失效
解决方案:
$("#table").on("click","td",function(){ alert($(this).attr("id")); });
js给<asp:Label 赋值,以及赋了值而后台却取不到-解决方案
html:
<asp:Label ID="lbl_Test" runat="server" CssClass="cssTest" Text="测试" ></asp:Label>
1.解决赋值问题。
在前端使用Js给ASP.NET服务器控件<asp:Label 赋值时,因为取不到ID值,所以无法通过ID直接获取到该控件对象,这时只要设置控件属性CssClass=“cssTest” 即可,这样Js就可以直接通过类名找到该对象了。
visilbe 为false的也获取不到
$("#btnaddbaby").click(function () {
var monitorid = $(this).attr("id");
console.log($(this).attr("id"));
});
ddl 动态添加options
var htmlInfo = "";
//ddlbaby
$.each(obj, function (n, value) {
htmlInfo += " <option value=" + value.BabyKey + ">" + value.BabyName + "/" + value.MotherName + "/" + value.RoomName + "</option>";
});
$("#ddlbaby").append(htmlInfo);
$(function () {
//获得绑定数据
$.ajax({
type: "POST",
url: "/ajax/BabyHandler.ashx",
dataType: "json",
data: {
act: 'getlist'
},
success: function (e) {
if (e.ResultCode == 1) {
var obj = e.lstBaby;
var htmlInfo = "";
htmlInfo += `<div class=\"babycount\">共有${obj.length}个宝宝 </div>`;
htmlInfo += ` <div class="contentwrap">`;
$.each(obj, function (n, value) {
htmlInfo += ` <div class="monitor_box">`;
htmlInfo += ` <div class="monitorroominfo" style=""><span class="monitornameandroom">房间:${value.RoomName},妈妈:${value.MotherName}</span><a href="#" id=${value.BabyKey} οnclick='deletbaby(${value.BabyKey})' >删</a></div>`;
htmlInfo += ` <div class="monitorbabyinfo"><span class="monitorbabyname">${value.BabyName}</span><span class="monitorbabyid">(ID${value.BabyKey})</span></div>`;
htmlInfo += ` </div>`;
});
htmlInfo += ` <div class="monitor_box " style=" width: 80px; padding-left: 10px; padding-right: 10px; ">`;
htmlInfo += ` <div id ="btnaddbaby" class="btnaddbaby" οnclick='addbaby()'> </div>`;
htmlInfo += ` </div>`;
htmlInfo += ` </div > `;
$("#babycontent").html(htmlInfo);
} else {
alert(e.ResultMessage);
}
});
$("#btnaddbaby").click(function () {
var monitorid = $(this).attr("id");
console.log($(this).attr("id"));
$(".showpop").show();
$(".bindbaby").hide();
$(".showbindbabywrap").hide();
$(".addbabywrap").show();
});
$("._monitorempty").click(function () {
var monitorid = $(this).attr("id");
console.log($(this).attr("id"));
$(".showpop").show();
$(".bindbaby").show(); $(".addbabywrap").hide();
$(".showbindbabywrap").hide();
$(".showpop_manitor_name").text(monitorid);
});
$("._monitorbaby").click(function () {
var monitorid = $(this).attr("id");
console.log($(this).attr("id"));
$(".showpop").show();
$(".bindbaby").hide(); $(".addbabywrap").hide();
$(".showbindbabywrap").show();
$(".showpop_manitor_name").text(monitorid);
});
$(".close").click(function () { $(".showpop").hide(); });
});