php - 如果输入了行中的一个字段,则该行中的所有其他字段都是必填项

标签 php javascript jquery

这是输入日期、选择进/出和位置的代码。 如果用户想要输入更多字段,我还添加了一个 addRow 函数。

<table>    
for($i=0;$i<15;$i++){ 
<tr><td>
<input type='datepick' name='scheduledatepick[$i]' />
<select name='schedulein[$i]' /><option>--</option>
<input type='text' name='location[$i]' />
</td></tr>
}
</table>

现在我的问题是,如果用户在一行中输入一个字段(可能是日期选择或日程安排或位置),那么他必须在同一行中输入所有其他字段。如何做到这一点? enter image description here

最佳答案

假设您希望在点击按钮 时发生这种情况,您可以这样做: Working Demo

jQuery

$('button').click(function() {
    // set up an array to store the invalid rows
    var rows = new Array();
    $('table tr')
        // reset all rows before we validate
        .removeClass("error")
        // loop over each row
        .each(function(i) {
            // work out whether the fields are completed or not
            var filledFieldCount = 0;
            filledFieldCount += $("[name='scheduledatepick[" + i + "]']", this).val().length > 0 ? 1 : 0;
            filledFieldCount += $("[name='schedulein[" + i + "]']", this).val() !== "--" ? 1 : 0;
            filledFieldCount += $("[name='location[" + i + "]']", this).val().length > 0 ? 1 : 0;

            // if the total completed fields for this row
            // is greater than none and less than all
            // then add the row to the invalid rows list
            if (filledFieldCount > 0 && filledFieldCount < 3) {
                rows.push(this);
            }
         });

    // finally, change the background of the
    // rows to mark them as invalid
    if (rows.length > 0){
        $(rows).addClass("error");
    }

});

CSS

.error { background-color: red; }

关于php - 如果输入了行中的一个字段,则该行中的所有其他字段都是必填项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6443208/

相关文章:

javascript - 如何将 jQuery MultiSelect 小部件设置为只读?

jQuery 获取所有没有 class 属性的 div

php - PDO - 动态绑定(bind)参数 - 值为 0 奇怪的结果

php - 从 Paypal 站点返回后,Firefox 不返回 POST 数据

javascript - 为什么 crypto.createHash 在新版本中返回不同的输出?

javascript - 如何使 JSON 数组唯一

javascript - HTML5 数字输入 - 显示为百分比而不是小数

php - 使用 PHP 和 SQL 对调查问卷数据进行排序

javascript - for 循环将 javascript 变量设置为 php 变量

javascript - 使用 jQuery 打印日语