我正在使用引导程序创建一个博客,并且我有一个提交类别的表单:
<form action="categories.php" id="category-form" class="form-horizontal" role="form" method="post">
<div class="form-group">
<label for="category" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
<input type="text" name="category" class="form-control" id="category" placeholder="Category">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="submit" id="btn-submit" class="btn btn-primary" value="Add Category">
</div>
</div>
</form>
当我按下表单按钮“添加类别”时,对话框出现,但几秒钟后它立即提交,对话框消失,没有单击按钮“是”或“否”,在网上搜索我找到了一些解决方案,但不起作用为了我。我用于 Alertify JS 的代码如下:
$("#btn-submit").on("click", function(){
alertify.confirm("This is an alert dialog?", function(e){
if (e) {
alertify.success("Category was saved.")
} else {
alertify.error("Category not saved.");
}
});
return false;
});
我也尝试event.preventDefault();
:
$("#btn-submit").on("click", function(event){
event.preventDefault();
alertify.confirm("This is an alert dialog?", function(e){
if (e) {
$("#category-form").submit();
alertify.success("Category was saved.")
return true;
} else {
alertify.error("Category not saved.");
return false;
}
});
});
但也不起作用。请提供任何帮助...谢谢。