我没有得到发布的输入值

时间:2015-11-30 06:29:44

标签: javascript jquery

我使用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>

3 个答案:

答案 0 :(得分:2)

您需要获取click事件中的输入值。目前你在dom准备就绪,当时它是空的。然后你使用那​​个空值来创建元素。

collapsingToolbar.setContentScrimColor(Color.TRANSPARENT);
collapsingToolbar.setStatusBarScrimColor(Color.TRANSPARENT);

Fiddle

答案 1 :(得分:0)

这可能是打字错误

$(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--;
})

FIDDLE

答案 2 :(得分:0)

下面的片段工作正常。

    $(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>");
    }
});

查看fiddle