我试图通过jquery获取动态生成的标签元素的文本值。但它显示为空白。如何实现它任何想法都将被赞赏。
动态生成的代码
<script type="text/javascript">
$(document).ready(function () {
$("#openForm").click(function () {
var radioValue = $("input[name='optionsRadios']:checked").val();
if (radioValue == 'uncoatedRdBtn') {
$("#pdfForm").prepend("<div id='pdfFormInsideL1'></div>");
$("#pdfFormInsideL1").prepend("<h3><strong>helm</strong></h3><h3 id='pdfFormInsideL2'><strong> Delivery Conditions (TDC)</strong></h3>")
$("h3").addClass("text-center");
$("#pdfFormInsideL2").after("<div id='pdfFormInsideTblRight'></div>");
$("#pdfFormInsideTblRight").addClass("table-responsive pull-right");
$("#pdfFormInsideTblRight").css("width:300px");
$("#pdfFormInsideTblRight").append("<table class='table table-bordered'><thead><tr><th class='text-center'>TDC No.</th>" +
"<th><input id='Tdc_No' type='text' value=''/></th></tr></thead>" +
"<tr><td class='text-center'>Revision</td>" +
"<td><label id='Revision' value='0'></label></td></tr>" +
"<tr><td class='text-center'>Date</td>" +
"<td><label id='Revision_Date'></label></td></tr></table>");
var date = new Date();
var val = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
$("#Revision_Date").text(val);
var s = $(' <input type="button" ID="btnFrmSubmit" runat="server" Text="Submit" value="FrmSubmit" />');
$("#btnExport").after(s);
}
});
});
</script>
通过单击上面脚本中的“btnFrmSubmit”按钮访问标签值的代码
<script type="text/javascript">
$(function () {
$(document).on("click", "[id*=btnFrmSubmit]", function () {
alert("hi");
var user = {};
//user.Product_Id = $("[id*=Product_Id]").val();
user.Tdcno = $("[id*=Tdc_No]").val();
user.Revision = $("[id*=Revision]").val();
user.Revision_Date = $("[id*=Revision_Date]").val();
user.Created_Date = $("[id*=Revision_Date]").val();
user.Created_By = $('#form1').find('input[ID="lblUserName"]').val();
</script>
答案 0 :(得分:0)
只需使用.text()
获取标签的文本值
var value="Par";
$("#hId").append('<label id="ID_item">' + value + '</label><br /><hr />');
$("#hId").on('click','#ID_item', function(){
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="hId"></div>