Jquery 检查强制下拉列表
在激活ajax并将数据发送到C中的webmethod之前,我需要检查一些值是否已填充到数据中。 在我的选项卡中有一个隐藏列,它的值为true或false,我编写了jquery循环来检查它们行中的文本框是否是必需的。这个很好用。 现在,我必须对3个下拉字段Product Level1、Level2和Level3执行相同的操作。 我设法写了一些代码,但效果不好,警报系统混乱不堪。 首先,当点击按钮时,代码需要检查表中的强制文本框字段,然后检查是否填写了所有3个下拉列表,然后启动ajax 这是我的表格和代码: 这里是jquery部分:Jquery 检查强制下拉列表,jquery,html,Jquery,Html,在激活ajax并将数据发送到C中的webmethod之前,我需要检查一些值是否已填充到数据中。 在我的选项卡中有一个隐藏列,它的值为true或false,我编写了jquery循环来检查它们行中的文本框是否是必需的。这个很好用。 现在,我必须对3个下拉字段Product Level1、Level2和Level3执行相同的操作。 我设法写了一些代码,但效果不好,警报系统混乱不堪。 首先,当点击按钮时,代码需要检查表中的强制文本框字段,然后检查是否填写了所有3个下拉列表,然后启动ajax 这是我的表格
$(function () {
$(".IDMandatory").each(function (i, el) {
if ($(el).text().toUpperCase() === "TRUE") {
$(el).closest("tr").find("input,select").prop("required", true);
}
});
$("#myButton").on("click", function () {
var ok = true;
$("[required]").each(function () {
$(this).css("border", ""); // reset
if (!$(this).val()) {
ok = false;
$(this).css("border", "2px solid red");
var message = "";
if ($("#MainContent_ddlProductHierarchyLvL1").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 1 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL2").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 2 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL3").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 3 field!";
}
if (message != "") {
alert(message);
}
}
});
if (ok) {
}
else {
alert("Fill in the remaining mandatory fields!");
}
});
});
我认为问题在于:
var message = "";
if ($("#MainContent_ddlProductHierarchyLvL1").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 1 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL2").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 2 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL3").val() == 0) {
message += "Plase fill in the GPR PIM Product Hierarchy Level 3 field!";
}
if (message != "") {
alert(message);
}
有人能帮我查一下这个密码吗
提前谢谢 您可以使用以下命令验证选择框:
将消息构造置于$[required]之外。每个函数都应该做到这一点
$(function () {
$(".IDMandatory").each(function (i, el) {
if ($(el).text().toUpperCase() === "TRUE") {
$(el).closest("tr").find("input,select").prop("required", true);
}
});
$("#myButton").on("click", function () {
var ok = true;
$("[required]").each(function () {
$(this).css("border", ""); // reset
if (!$(this).val()) {
ok = false;
$(this).css("border", "2px solid red");
}
});
var message = "";
if ($("#MainContent_ddlProductHierarchyLvL1").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 1 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL2").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 2 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL3").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 3 field!";
}
if (message != "") {
alert(message);
}
if (ok) {
}
else {
alert("Fill in the remaining mandatory fields!");
}
});
});
问题不在于message+=代码,而在于$[required].eachfunction{-您通过查看命名的/id'd元素和常规处理$[required]来混合显式检查感谢您的回复!这很好,但有一个问题。当填写表中的文件和三个ddl时,它应该触发ajax:/现在,只有填写表中的字段时,ajax才会触发。我发出了一个警报来模拟ajax:您能帮上忙吗?@freej17如果您希望ajax只在填写必填字段和ddl时触发,当ddl的每个值都为0时,您可以将“确定”设置为false。请查看编辑后的代码段。非常感谢!这项工作非常好,只剩下一件小事。即使在表格中填写了警告必填字段,也会触发这些字段:/Thank you's efforce!:我将尝试修复最后一个问题!感谢您的重播!但是代码如果同时填写了表中的所有文件,则应首先检查表中的所有文件,然后检查3个下拉列表,然后在一切正常后启动ajax。我更新了代码段。现在的问题是,当只检查表中的文件时,ajax将启动,而不是等待下拉列表检查。您能提供帮助吗?
$(function () {
$(".IDMandatory").each(function (i, el) {
if ($(el).text().toUpperCase() === "TRUE") {
$(el).closest("tr").find("input,select").prop("required", true);
}
});
$("#myButton").on("click", function () {
var ok = true;
$("[required]").each(function () {
$(this).css("border", ""); // reset
if (!$(this).val()) {
ok = false;
$(this).css("border", "2px solid red");
}
});
var message = "";
if ($("#MainContent_ddlProductHierarchyLvL1").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 1 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL2").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 2 field!";
}
if ($("#MainContent_ddlProductHierarchyLvL3").val() == 0) {
ok = false;
message += "Plase fill in the GPR PIM Product Hierarchy Level 3 field!";
}
if (message != "") {
alert(message);
}
if (ok) {
}
else {
alert("Fill in the remaining mandatory fields!");
}
});
});