我对为什么这不起作用感到困惑。我只想在请求成功后清除文本框。成功设置的功能是否错误?我刚开始使用AJAX和JavaScript,因此任何建议都很棒! 这是完整的功能,可让您更全面地了解整体功能。
function txtChk() {
var controls = 0;
/* -----------------------------------------------
DECLARING OBJECTS TO HOLD ALL CONTROLS
----------------------------------------------- */
const labels = document.getElementsByClassName('inputLabel');
const texts = document.getElementsByClassName('gezza-field');
var rdbYes = document.getElementById("<%=rdbYes.ClientID%>");
var rdbNo = document.getElementById("<%=rdbNo.ClientID%>");
var rdbYesLead = document.getElementById("<%=rdbYesLead.ClientID%>");
var rdbNoLead = document.getElementById("<%=rdbNoLead.ClientID%>");
var first = document.getElementById("<%=txtFirst.ClientID%>");
var last = document.getElementById("<%=txtLast.ClientID%>");
var phone = document.getElementById("<%=txtPhone.ClientID%>");
var email = document.getElementById("<%=txtEmail.ClientID%>");
const radiosRef = document.getElementsByClassName('referral');
const radiosLead = document.getElementsByClassName('lead');
/*--------------------------------------------------------
RUNNING FOR LOOPS THROUGH DECLARED OBJECTS TO
HANDLE ANY ERRORS (TERNARY OPERATORS INSIDE FORS)
(SECOND EDIT)
------------------------------------------------------- */
for (var i = 0; i < 4; i++) {
labels[i].style.color = (texts[i].value == "" ? "FireBrick" : "Black");
labels[i].innerHTML = (texts[i].value == "" &&
labels[i].innerHTML.charAt(0) != "*" ? "*" + labels[i].innerHTML : labels[i].innerHTML);
}
labels[4].innerHTML = (rdbYesLead.checked == false && rdbNoLead.checked == false ? "*Please select one" : "");
controls = (rdbYesLead.checked == false && rdbNoLead.checked == false ? controls : controls + 1);
labels[5].innerHTML = (rdbYes.checked == false && rdbNo.checked == false ? "*Please select one" : "");
controls = (rdbYes.checked == false && rdbNo.checked == false ? controls : controls + 1);
for (var l = 0; l < 4; l++) {
if (texts[l].value != "") {
controls += 1;
}
}
if (controls < 6) {
alert("Fix all errors before submitting");
return false;
}
else {
var referral = rdbYes.checked == true ? 1 : 0;
var lead = rdbYesLead.checked == true ? 1 : 0;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/updateDB",
data: "{'first':'" + first.value + "','last':'" + last.value + "','phone':'" + phone.value + "','email':'" + email.value + "','referral':'" + referral + "','lead':'" + lead + "'}",
dataType: "json",
success: function (response) {
alert("Thank you, " + first.value + "!");
first.value = "";
last.value = "";
phone.value = "";
email.value = "";
},
error: function () {
alert("Error! Connection was lost, please try again");
}
});
//first.value = "";
//last.value = "";
//phone.value = "";
//email.value = "";
//rdbNo.checked = false;
//rdbYes.checked = false;
//rdbNoLead.checked = false;
//rdbYesLead.checked = false;
}
}