[英]I am not getting the posted input value
我使用jquery和javascript创建了一个添加任务,但是当我键入内容并单击添加任务时,该值未显示,我尝试过,但对我不起作用,请告诉我是否有可能或我做错了什么
$(document).ready(function() {
var max_fields = 10;
var wrapper = $(".task");
var add_button = $(".add_task");
var a = document.getElementById('input').value;
document.getElementById("set").innerHTML = a;
var x = 1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++; //text box increment
$(wrapper).append("<div id='set'><p>"+a+"</p><a href='#' class='remove'>Remove</a></div>");
}
});
$(wrapper).on("click",".remove", function(e){
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
我的html代码
<div class="task">
<input type="text" value="" name='input' id="input" />
<button class="add_task">Add New Task</button>
<div id='set'><p></p></div>
</div>
您需要在click事件中获取输入的值。 目前,您是在准备好dom时获取它的,当时它是空的。 然后,您将使用该空值创建元素。
$(add_button).click(function (e) {
e.preventDefault();
a = $("#input").val();
if (x < max_fields) {
x++; //text box increment
$(wrapper).append("<div id='set'><p>" + a + "</p><a href='#' class='remove'>Remove</a></div>");
}
});
这可能是打字错误
$(wrapper).on("click",".remove", function(e){
e text //what is the use of this remove it then check it will work fine
e.preventDefault(); $(this).parent('div').remove(); x--;
})
下面的片段工作正常。
$(add_button).click(function (e) {
e.preventDefault();
if (x < max_fields) {
x++; //text box increment
var a = document.getElementById('input').value;
$(wrapper).append("<div id='set'><p>" + a + "</p><a href='#' class='remove'>Remove</a></div>");
}
});
查看小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.