以下是我如何创建动态文本框的代码。我想验证创建的任何动态文本框是否为空,如果是,应该向用户提示错误消息或提示,以提示填写Additem按钮单击上的文本框。我收到输入到该函数中的警报消息,之后我没有收到任何警告用户填写文本框“xxxx”的消息。虽然动态文本框为空,但不会弹出警报消息
请帮我解决
if (ControlType == "TextBox")
{
int textBoxLength;
TextBox MynewTextBox = new TextBox();
MynewTextBox.ID = "txt" + Fldname;
MynewTextBox.Width = 100;
MynewTextBox.BorderStyle = BorderStyle.Solid;
MynewTextBox.Attributes.Add("Type", "T");
MynewTextBox.Attributes.Add("IsKeyField", "Y");
MynewTextBox.Attributes.Add("IsMandatory", "Y");
}
protected void Page_Load(object sender, EventArgs e)
{
btnAddItem.Attributes.Add("onClick", "if(!ValidateMandatoryFields()) return false;");
}
<script language="javascript" type="text/javascript">
function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
for (var i = 0; i < inputControls.length; i++) {
if (inputControls[i].getAttribute("IsKeyField") == "Y") {
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C")) {
if (inputControls[i].getAttribute("IsMandatory") == "Y") {
if (inputControls[i].value == "") {
alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
}
}
}
}
}
}
</script>
enter link description here
我按照上面
链接的代码它仍然没有帮助我
function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
for (var i = 0; i < inputControls.length; i++) {
if (inputControls[i].getAttribute("IsKeyField") == "Y") {
alert(inputControls[i]);
}
else {
alert("first if statemenet");
}
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C")) {
alert(inputControls[i]);
}
else {
alert("second if statement");
}
if (inputControls[i].getAttribute("IsMandatory") == "Y") {
alert(inputControls[i]);
}
else {
alert("third if statement");
}
if (inputControls[i].value == "") {
alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");--error here
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
else {
alert("name: " + inputControls[i].id);
alert("length: " + inputControls[i].value.length());
alert("value: " + inputControls[i].value);
}
}
}
它弹出所有其他状态的变化ents出现,最后我得到一个错误,指出Microsoft JScript运行时错误:对象预期。这意味着,没有我,如果条件为真...我如何得到它的工作......请帮我用正确的逻辑
@JColeson,我添加了一些如你所说的代码,它仍然不给出任何警报的名称,长度和价值...不知道我错过了什么 – Janet 2011-04-18 19:27:21
它显示为:alert(inputControls.length); 你的浏览器是否说有没有页面上的脚本错误? – jColeson 2011-04-18 19:33:03
89 for inputControls.Length,并且浏览器上没有错误。 – Janet 2011-04-18 19:42:47