无法按预期运行Javascript

时间:2018-05-01 16:39:30

标签: javascript jquery



$(document).ready(function(){
	$(".add-item-popup").click(function(){
		var form = "<input type='text' class='add-item-inp' />";
		$.alert(form);
	});

	$(".add-item-inp").change(function(){
		alert($(this).val());
	});
});
&#13;
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>
</head>
<body>
<a href="#" class='add-item-popup'>Add</a>
</body>
</html>
&#13;
&#13;
&#13;

我在弹出窗口中有一个输入字段,我试图在输入改变后抓取输入值。但是我无法获取我在弹出窗口中通过jquery添加的输入字段的值。请帮忙

1 个答案:

答案 0 :(得分:1)

尝试委托来自body&amp ;;的活动如果需要获取每个键上的值

,请使用keyup

$(document).ready(function() {
  $(".add-item-popup").click(function() {
    var form = "<input type='text' class='add-item-inp'/>";
    $.alert(form);
  });

  $("body").on('keyup', ".add-item-inp", function() {
    console.log($(this).val());
  });
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>

<a href="#" class='add-item-popup'>Add</a>