我已經使用html和jquery創建了一個表單。它連接到一個數據庫,它應該在數據庫中輸入寫在文本框中的值。但我有以下問題。如果某個特定文本框沒有鍵入值,則應該發出警報,並防止數據進入數據庫。它進展順利。但是,我想要一個文本框,如果這個文本框的值不等於其他值的總和,它應該會提醒,並且對db執行相同的操作。這就是問題所在。它沒有得到正確的總和。我的意思是,如果我在第一個文本框中的值爲15,然後在其他文本框中,值爲10,然後,值爲5 ..(10 + 5等於第一個文本框),它不應該警惕,並做它需要在db中輸入這個數據。但事實並非如此。它提醒等。我怎樣才能從文本框中獲得這筆錢?謝謝不能正確計算文本框值
function checkForm() {
if ($('#nr_articol').val().length === 0) {
alert("Nr articol trebuie sa fie introdus! ");
return false;
}
return true;
}
function checkForm2() {
if ($("#scurt_smd").val() + $("#incomplete_smd").val() + $("#bile").val() + $("#scurt_val").val() + $("#incomplete_val").val() + $("#lipsa_smd").val() + $("#invers_smd").val() + $("#componente_lipsa").val() + $("#componente_inversate_1").val() + $("#lipire_hotbar").val() + $("#componente_inversate_2").val() + $("#componente_inversate_3").val() + $("#componente_inversate_4").val() != $('#optic').val() || $('#electric').val()) {
alert("Ai introdus mai multe sau mai putine bucati decat ai verificat in total. Verifica inca odata!");
return false;
}
return true;
}
$(document).ready(function() {
$('#submitbtn').click(function() {
if (checkForm() == true) {
if (checkForm2() == true) {
var frm = $('#form');
// var data = JSON.stringify(frm.serializeArray());
var data = frm.serializeArray();
console.log(data);
$.ajax({
type: 'post',
url: 'http://localhost:3000/',
contentType: 'application/json',
crossDomain: true,
data: JSON.stringify(data),
dataType: 'json',
success: function() {
alert('sucessfully inserted');
},
error: function (err) {
console.log(err);
}
});
}
}
});
});
您首德使用'Number'或'parseInt'添加輸入值之前投入的價值轉換爲'number'。因爲它們的默認值應該是'string'。 – fuyushimoya