2011-06-22 31 views
3

这是输入日期,选择输入/输出和位置的代码。 如果用户想要输入更多字段,我还添加了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> 

现在我的问题是,如果用户连续输入的字段(也许datepick或schedulein或位置),那么他就必须输入同一行中的所有其他领域。如何实现这一目标? enter image description here

+0

@fina lForm你认真吗? – schaitanya

+1

看起来像是一个问题... – Town

+0

@Town他重新编辑它,以包括一个问题。 – FinalForm

回答

1

假设你想这是一个按钮点击发生,你可以这样做: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; } 
+0

优秀。这是我正在寻找的那个。必须稍作修改才能为我工作。 http://jsfiddle.net/3xDAx/ – schaitanya

1

PHP端:

$errs = array(); 
for ($i = 0; $i < 15; $i++) { 
    $cnt = empty($_REQUEST['scheduledatepick'][$i]) + empty($_REQUEST['schedulein'][$i]) + empty($_REQUEST['location'][$i]); 
    if ($cnt > 0) && ($cnt != 3) { 
     $errs[] = "Row $i not completed"; 
    } 
} 
if (count($errs) > 0) { 
    ... at least one incomplete row 
} 

Javascript的面可能会有所等价的,额外的代码来处理的选择,复选框,文本框,文本域等之间的差异......

+0

当然,这只是检查_any_空字段,而不是至少填充一个字段的行上的空字段? – Town

+0

嗯。真的够了。 –

+0

在那里,应该做到这一点。 –

0

你需要解析表单。
如果一行不完整,则向用户抛出错误消息。
这一切都在Javascript中。

然后你必须在PHP中执行相同的检查。 Javascript检查方便,可以立即回复用户,但很容易被中和。

相关问题