【发布时间】:2014-04-28 00:55:33
【问题描述】:
我正在使用 PHP、MySQL 和 jQuery 创建一个简单的任务管理器应用程序。我正在添加一项功能,允许用户通过单击“新任务”按钮、在文本字段中输入并按 Enter 来添加任务。这是我拥有的 jQuery:
function add_task() {
// Add the "add new task" button
$("<span class='add-new'>Add new item</span>").appendTo(".sub-phase");
// String for the input form
var task_form = '<form class="add-new-task" autocomplete="off"><input type="text" name="new-task" placeholder="Add a new item..." /></form>';
// Display form on button click
$('.add-new').click(function() {
$(task_form).appendTo($(this).parent());
});
// Post results
$('.add-new-task').submit(function(){
var new_task = $('.add-new-task input[name=new-task]').val();
if(new_task != ''){
$.post('includes/add-tasks.php', { task: new_task }, function( data ) {
$('.add-new-task input[name=new-task]').val('');
$(data).appendTo('.task-list').hide().fadeIn();
});
}
return false;
});
}
如果我在 index.php 文件中硬编码了表单,则此功能可以按预期工作。但是如果表单是在 jQuery 中创建的,就像我在这里一样,那么提交时不会发生任何事情。我四处搜索,但未能找到可行的解决方案。
非常感谢任何建议!谢谢。
【问题讨论】:
-
你试过 $('').addClass('add-new');
标签: javascript php jquery mysql forms