我做了某种形式的验证。用户可以输入组名和组描述。有了jQuery,我正在验证组名是否为空,如果空提交按钮应该被禁用。现在我遇到了禁用提交按钮的问题。如果我点击组名称的输入标签,那么验证是可以的,并且提交按钮被禁用,但是如果我只是点击提交按钮,而不触及其他任何东西,那么jQuery跳过验证并激发提交按钮,尽管组名称为空。 我试着用jQuery设置输入标签的焦点,但它只有在我实际上点击该输入标签时才起作用。禁用提交按钮jQuery验证
提交按钮是'saveGroup'按钮。
有人可以告诉我如何在这个输入标签上调用点击事件,或者我可以使用其他验证技巧。
<div class="newGroupDiv">
<label>Title: </label><input type="text" id="groupTitle" onblur="checkTitle();"><br>
<label>Description:</label><br>
<textarea id="groupDescription"></textarea><br><br>
<button id="saveGroup">Save</button>
<button id="cancelGroup">Cancel</button>
<label id="groupError"></label>
</div>
---------------------------------------------------------------------------------
$("#saveGroup").click(function(){
var variable = checkTitle();
if(variable == true){
if($("#groupError").html() == ""){
$(".columns").append('<ul class="'+ $("#groupTitle").val() +'"><li class="naslov">'+ $("#groupTitle").val() +'</li></ul>');
$("ul").sortable({containment : 'document',
tolerance: 'pointer',
cursor: 'pointer',
revert: 'true',
opacity : 0.6,
connectWith : "ul",
placeholder: 'border',
items : 'li:not(.naslov)',
start : function(){
check = false;
$(".readContent").fadeOut(300);
}, stop : function(){
check = true;
}}).disableSelection();
$.post("addGroup.php", {'title' : $("#groupTitle").val(), 'description' : $("#groupDescription").val(),
'color' : $("#colorHex").html(), 'color2' : $("#colorHex2").html()}, function(){
window.location.reload();
});
}
}
});
--------------------------------------------------------------------------------
var checkTitle = function(){
$.post("checkTitle.php", {'title' : $("#groupTitle").val()}, function(data){
if(data == 'exist') $("#groupError").html("Group already exists");
if(data == 'no title') $("#groupError").html("Group title can't be empty");
else if(data == 'ok') $("#groupError").html("");
});
return true;
}
有了这个“变量”我试图完成某种回调等待,所以当这个变量会从函数的结果应该继续的代码休息,但我不知道它是否工作。
只是一个旁注,检查标题是否为空可以在不触发ajax调用的情况下完成。只是为了节省一些时间,带宽和速度:) –
哦,并为验证创建一个完整的函数,然后将你的函数连接到$(“#form”)submit(function(){});处理程序和$('#button')。点击(function(){});处理程序,所以它启动了更改组和验证提交表单 –
好吧,我没有实际使用表单,我制作了由jQuery驱动的div。而你的建议很有用,我没有Ajax调用进行标题验证,它的工作原理。所以我的checkTitle()函数现在看起来像这样:http://pastebin.com/pXapRci9谢谢 – Alen