0

我想验证动态创建的文本区域,在下面提到的代码中,我只能验证第一行,但无法验证第二行。如何验证/获取所有行值。

提前致谢。

要创建动态元素,请单击添加问题图像。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
    <script type="text/javascript">
    $(document).ready(function ()
    {
        var i = 0;
        $("#AddQuestion").click(function ()
        {
            $("#NoQuestions").remove();
            ++i;
            var IdCount = i + ".)";
            var newRowContent = "<tr id='SQRow"+i+"'"+" ><td>" + IdCount + "</td>" + "<td><textarea id='txtQuestions" + i + "'" + "style='height: 45px;width: 420px'></textarea> </td>" + "<td><select id='ddlDataType" + i + "'" + " style='margin-left: 47px'><option value=''>Select Data Type</option><option value='int'>Numeric</option><option value='Varchar'>Alpha Numeric</option></select> </td>" + "<td><div ><a href='#'><img src='/Images/1363247672_document-edit.png' width='26' height='27' alt='EditButton'/> </a><a href='#'><img src='/Images/1363247321_Remove.png' alt='DeleteButton'/></a> </div> </br><div  style='display: none'><a href='#'><img src='/Images/Accept-iconSmall.png' width='26' height='27' alt='UpdateButton'/></a><a href='#'><img src='/Images/Button-Cancel-icon.png' width='26' height='27' alt='CancelButton'  /></a></td>" + "</tr>";

            $("#tblSecurityQuestions").append(newRowContent);
        });
        $("#btnUpdateQuestions").click(function ()
        {
            var txtAreaVal = $('textarea').val().length;
            var ddlDataType = $('#tblSecurityQuestions select').val().length;

            alert(txtAreaVal);
            alert(ddlDataType + "The ddl is " );
        if (txtAreaVal <= 0)
        {
            alert('Please add Questions');
            return;
        } else if (ddlDataType <= 0)
        {
            alert('Please select the data type');
            return;
        }
        });

    });
    </script>
</head>
 <body>
    <form id="SQPageForm" name="SQPageForm" method="post" action="">
            <div id="SecurityQuestions" style="height: auto;border-color: #f00;border: 1.5px;border-style: dotted">

               <h3>Configure Security Questions</h3>
                <div id="AddNewSecurityQuestions" style="">
                    <p style="float: left;margin-top: 11px">Add Question </p>

                   <img id="AddQuestion" src="/Images/document-add-icon.png" alt="Add Questions" width="35px" height="35px" style="cursor: pointer"/>
                </div>
                </br>
              &nbsp;Security  Questions?
                </br>

                <table border="1" id="tblSecurityQuestions">
                    <tr>
                        <th style="width: 25px">
                            ID
                        </th>
                        <th style="width: 424px">
                            Security Questions
                        </th>
                        <th style="width: 210px">
                            DataType
                        </th>
                        <th style="width   :65px">
                            Actions
                        </th>

                    </tr>
                      </table>
                     </div>
                </br>
                </br>
                <input type="button" id="btnUpdateQuestions" value="Set Security Question" style="margin-left: 300px" />
 </body>
</html>
4

3 回答 3

0

尝试迭代所有文本区域,然后检查相同的条件:

for(var i=0; i<NoOfquestions; i++){

    var txtAreaVal = $('#txtQuestions'+i).val().length;
    var ddlDataType = $('#tblSecurityQuestions select').val().length;

    alert(txtAreaVal);
    alert(ddlDataType + "The ddl is");

    if (txtAreaVal <= 0)
    {
        alert('Please add Questions');
        return;
    }
    else if (ddlDataType <= 0)
    {
        alert('Please select the data type');
        return;
    }
}
于 2013-03-19T04:10:55.473 回答
0

尝试

$("#btnUpdateQuestions").click(function() {
    $('#tblSecurityQuestions tr:gt(0)').each(function() {
        var $this = $(this);
        var txtAreaVal = $('textarea', $this).val().length;
        var ddlDataType = $('select', $this).val().length;

        alert(txtAreaVal);
        alert(ddlDataType + "The ddl is ");
        if (txtAreaVal <= 0) {
            alert('Please add Questions');
            return;
        } else if (ddlDataType <= 0) {
            alert('Please select the data type');
            return;
        }
    })
});
于 2013-03-19T04:19:47.530 回答
0
var textAreaSize = $('#tblSecurityQuestions tr:gt(1)').size();

        for (var j = 1; j <= textAreaSize; j++)
        {
            var txtAreaVal = $('#txtQuestions' + j).val();
            var ddlDataType = $('#ddlDataType' + j).val();  
           if (txtAreaVal <= 0)
            {
                alert('Please add Questions');
                return;
            } else if (ddlDataType <= 0)
            {
                alert('Please select the data type');
                return;
            }
        }

我想出了一个解决方案,它现在对我有用

于 2013-03-19T07:02:51.823 回答