[英]jquery ajax call on button click validation not working
我正在使用jquery ajax調用在btn單擊上提交表單。 我正在做表單驗證,如果輸入無效,則需要停止ajax調用。 我嘗試了以下代碼,但是即使條件失敗,表單也會每次提交
<script type="text/javascript">
$(document).ready(function () {
$('#btnSubmit').click(function (evt) {
if (ValidateUserInput()) {
$.ajax({
type: 'GET',
url: '@Url.Action("SearchResults", "Request")',
data: { searchTerm: $("#searchTerm").val(), searchFilter: $("#searchFilter").val() },
success: function (data) {
$('#Results').html(data);
}
});
}
});
});
function ValidateUserInput() {
if ($('#searchFilter').val() == 'RequestId' && $('#searchTerm').val() != parseInt($('#searchTerm').val())) {
$('#HiddenInvalidInputContainer').show();
return false;
}
else {
alert('testing');
$('#HiddenInvalidInputContainer').hide();
return true;
}
}
</script>
有任何想法嗎?
您總是會得到else子句,因為對於一個數字而言,這始終是錯誤的
$('#searchTerm').val() != parseInt($('#searchTerm').val()
"1" != parseInt("1")
為假, "1" !== parseInt("1")
為true
如果您要測試非數字,則可以使用isNaN()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.