我做了某種形式的驗證。用戶可以輸入組名和組描述。有了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