我有这个 HTML 标记:
<div id="destination">
</div>
<input id="test" type="text" />
<button id="clicker">Click me</button>
还有这个 jQuery 片段
$(document).on('click', '#clicker', function(event){
var newCat = $('#test').val();
$(newCat).prepend("#destination");
alert(newCat);
});
警报文本确实是字段中输入的文本,但该文本最终并未出现在 div #destination
中。
查看这个 jsfiddle 示例: http://jsfiddle.net/FaAY4/
最佳答案
您必须这样做:
$(document).on('click', '#clicker', function(event){
var newCat = $('#test').val();
$("#destination").prepend(newCat );
alert(newCat);
});
关于jQuery,从输入前置数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13417490/