【问题标题】:Alert message not popping up though the dynamic textbox is empty尽管动态文本框为空,但未弹出警报消息
【发布时间】: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


    【解决方案1】:

    尝试添加一个 else 子句来查看值是什么:

    if (inputControls[i].value == "") {
       ... your code
    } else
    {
       alert("name: " + inputControls[i].id);
       alert("length: " + inputControls[i].value.length());
       alert("value: " + inputControls[i].value);
    }
    

    它们可能被设置为 null 或空格,而不仅仅是一个空字符串。

    【讨论】:

    • @JColeson,我已经按照你的建议添加了代码,它仍然没有给出任何关于名称、长度和值的警报......不知道我错过了什么
    • 它显示什么:alert(inputControls.length);您的浏览器是否会显示页面上是否有任何脚本错误?
    • 89 for inputControls.Length,浏览器上没有错误。
    • 将 else 子句添加到所有其他 if 语句中,看看哪个 If 语句返回 false。
    • 我已经用 else 语句和错误消息编辑了我上面的代码,...帮助我正确的逻辑...
    【解决方案2】:

    我能够修复我的代码,如下所示:

    function validateInput() {
    var arrTextBox = document.getElementsByTagName("input");
    var retValue = 1;
    var returnValue = 1;
    for (i = 0; i < arrTextBox.length; i++) {
        if (arrTextBox[i].type =="text" && arrTextBox[i].getAttribute("IsMandatory")=="Y" && arrTextBox[i].value == "") {
            retValue = 0;
        }
    }
    if (retValue == 0) {
        alert("Validation Failed");
        return false;
    }
    else {
        alert("Validation Success");
        return true;
    }
    

    在 ascx.cs 文件上

     btnAddItem.Attributes.Add("onclick", "{return validateInput()};");
    

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 1970-01-01
      • 2017-02-13
      • 2011-06-09
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多