【发布时间】:2011-08-08 01:40:45
【问题描述】:
下面是我如何创建动态文本框的代码。我想验证创建的任何动态文本框是否为空,如果是,则应向用户抛出错误消息或警报,提示用户在单击 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);
}
}
}
它会弹出所有其他语句,最后我收到一条错误消息,指出 Microsoft JScript 运行时错误:预期对象。这意味着我的 if 条件都不是真的......我如何让它工作......请帮助我正确的逻辑
【问题讨论】:
标签: asp.net