javascript - Javascript - JQuery无法从动态添加的输入中获取值

我想运行这个脚本:


<script>


 $(document).ready(function () {


 var sd = $("#StartDate").val();


 var ed = $("#EndDate").val();


 $("#userReportGenerate").click(function (e) {


 e.preventDefault();


 console.log(sd);


 console.log(ed);


 $("<input/>").attr("type","hidden").attr("id","uStartDate").attr("name","StartDate").attr("value", sd).appendTo("userReportForm");


 $("<input/>").attr("type","hidden").attr("id","uEndDate").attr("name","EndDate").attr("value", ed).appendTo("userReportForm");


 console.log($("#uStartDate").val());


 return false;


 });


 });


</script>



但是,当单击按钮时,我没有得到uStartDate ID的值,在控制台上显示为空白。

时间:


 $(document).ready(function () {


 var sd = $("#StartDate").val();


 var ed = $("#EndDate").val();


 $("#userReportGenerate").click(function (e) {


 e.preventDefault();


 let uStartDate='<input type="hidden" id="uStartDate" name="StartDate" value="'+sd+'">';


 $('#userReportForm').append(uStartDate); 


 return false;


 });


});



...