在javascript中使用for循环验证多个下拉列表

在javascript中使用for循环验证多个下拉列表,javascript,for-loop,Javascript,For Loop,我动态创建了多个for循环,并命名为act1[],act2[],act3[]。。截至act10[]。(可以算或不算10) 现在我想提交表单并获取下拉列表的所有值,但首先我想验证是否所有下拉列表的值都不等于“0”(我将标签放在value=0中 例: 选择活动。。 我尝试将“onsubmit”属性放在表单标记中,以便它在进入下一页之前先进入脚本。我已经放置了一个数组来计算创建的行数。但现在我正在进行验证。下面是我的代码: <script> function validateForm()

我动态创建了多个for循环,并命名为act1[],act2[],act3[]。。截至act10[]。(可以算或不算10)

现在我想提交表单并获取下拉列表的所有值,但首先我想验证是否所有下拉列表的值都不等于“0”(我将标签放在value=0中

例:

选择活动。。
我尝试将“onsubmit”属性放在表单标记中,以便它在进入下一页之前先进入脚本。我已经放置了一个数组来计算创建的行数。但现在我正在进行验证。下面是我的代码:

<script>
function validateForm(){
var table = document.getElementById('activityTable');
var rowCount = table.rows.length;

for(var a=1; a<=rowCount; a++){
    var dd = document.getElementById("act"+ a +"[]");
    var ddval = dd.value;

    if (ddval!=0){
        //dont know what to put
    }
    else{
        //don't know what to put
    }
}
}
</script>

函数validateForm(){
var table=document.getElementById('activityTable');
var rowCount=table.rows.length;

对于(var a=1;a,您可以在onsubmit中调用validateForm函数,并返回true(已验证)或false(未验证)值。如下所示:

<!--* to validate before form submit, include 'return' inside your onsubmit tag *-->
<form name='myForm' action='path/to/file.php' onsubmit='return validateForm()' 
method='post'>
    <!-- Make sure your form tags are completely outside your table,
         mixing them could result in invalid HTML which causes problems 
         (for best results, I'd ditch the table and use CSS instead) -->
<table>Your Table Here</table>
</form>

<script>
function validateForm(event){
    var table = document.getElementById('activityTable');
    var rowCount = table.rows.length;
    var allValid = true;

    for(var a=1; a<=rowCount; a++){
        var dd = document.getElementById("act"+ a +"[]");
        var ddval = dd.value;

        if (ddval!=0){
            allValid = true;
            // open the console so you can watch logs, this helps 
            // for troubleshooting but remove those in production
            console.log(a + ' returned true ' + allValid);
        }
        else{
            allValid = false;
            console.log(a + ' returned false ' + allValid);
            // you shouldn't need this, but if returning false
            //  doesn't work try adding preventDefault (you'll also
            //  have to pass 'event' into your function above) 
               event.preventDefault();
        }
    }
    // After you've looped through all rows, allValid will return true if
    // they were all true, or false if any of them were still 0.
    console.log('returning allValid as: ' + allValid;)
    return allValid; 
}
</script>

你的桌子在这儿吗
函数validateForm(事件){
var table=document.getElementById('activityTable');
var rowCount=table.rows.length;
var allValid=true;

对于(var a=1;aso到目前为止,这是可以的,但是如果表单返回false,如何使表单保持在同一页面上,如果返回true,如何使表单转到下一页面?您好,我已经编辑了我的答案,以添加更多的细节和注释:)
<!--* to validate before form submit, include 'return' inside your onsubmit tag *-->
<form name='myForm' action='path/to/file.php' onsubmit='return validateForm()' 
method='post'>
    <!-- Make sure your form tags are completely outside your table,
         mixing them could result in invalid HTML which causes problems 
         (for best results, I'd ditch the table and use CSS instead) -->
<table>Your Table Here</table>
</form>

<script>
function validateForm(event){
    var table = document.getElementById('activityTable');
    var rowCount = table.rows.length;
    var allValid = true;

    for(var a=1; a<=rowCount; a++){
        var dd = document.getElementById("act"+ a +"[]");
        var ddval = dd.value;

        if (ddval!=0){
            allValid = true;
            // open the console so you can watch logs, this helps 
            // for troubleshooting but remove those in production
            console.log(a + ' returned true ' + allValid);
        }
        else{
            allValid = false;
            console.log(a + ' returned false ' + allValid);
            // you shouldn't need this, but if returning false
            //  doesn't work try adding preventDefault (you'll also
            //  have to pass 'event' into your function above) 
               event.preventDefault();
        }
    }
    // After you've looped through all rows, allValid will return true if
    // they were all true, or false if any of them were still 0.
    console.log('returning allValid as: ' + allValid;)
    return allValid; 
}
</script>