2011-06-22 28 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檢查方便,可以立即回覆用戶,但很容易被中和。

相關問題