2013-01-05 23 views
-1

我正在使用此Javascript在表preq中添加行,其中已包含4行。問題是,當我使用這個Javascript添加行時,表格中正確添加了行,但是如果表單中出現了一些錯誤,並且如果表單重新載入比這個新添加的行不可見,並且我還希望在行內輸入的文本可見 enter image description here如果出現錯誤,顯示提交後新添加的行

javascript

<script type="text/javascript"> 
      var counter = 5; 
      function addrow() 
      { 
       if (counter > 10) 
       { 
        alert("Only 10 Kind of Works are allowed"); 
       return false; 
       } 
      $('#preq > tbody:last').append('<tr><td><input title="Enter Kind Of work" readonly="readonly" onclick="if(this.value!=\'\'){this.value=\'\';opendrop();}else{opendrop();}" id="other_work' + counter + '" name="other_work' + counter + '" type="text" size="30" onclick="opendrop()" <?php if (isset($errors)) { echo 'value="'.htmlentities(@$_POST['other_work' + counter + '']).'"'; } ?>></td><td><input name="client_name' + counter + '" type="text" id="client_name' + counter + '" size="40"/></td><td><input name="firm_name' + counter + '" type="text" id="firm_name' + counter + '" size="40"/></td></tr>'); 
       counter++; 
      } 
      </script> 

HTML代碼

<table align="left" id="preq" style="display: none;"> 
       <tr> 
           </tr> 
            <tr> 
            <th align="left"><label id="Label1"> Kind of Work </label></th> 
            <th align="left"><label id="Label1"> Name Of The Client </label></th> 
            <th align="left"><label id="Label1">Name of Firm/Organisation </label></th> 
            </tr> 
      <tbody> 

      <!--4 rows alredy present here--> 
      </tbody> 
    <tr><td colspan="3"><input type="button" class="button" value="Add Kind of Work" onclick="addrow()"/></td></tr> 

      </table> 
+1

研究*服務器端編程語言*(PHP)和*客戶端編程語言*(JavaScript)之間的區別。 – DCoder

回答

0

嘗試添加else,所以要添加,行僅當沒有錯誤(這是人對if()採取行動)。

if (counter > 10) { 
    alert("Only 10 Kind of Works are allowed"); 
    return false; 
} else { 
    $('#preq > tbody:last').append('<tr><td><input title="Enter Kind Of work" readonly="readonly" onclick="if(this.value!=\'\'){this.value=\'\';opendrop();}else{opendrop();}" id="other_work' + counter + '" name="other_work' + counter + '" type="text" size="30" onclick="opendrop()" <?php if (isset($errors)) { echo 'value="'.htmlentities(@$_POST['other_work' + counter + '']).'"'; } ?>></td><td><input name="client_name' + counter + '" type="text" id="client_name' + counter + '" size="40"/></td><td><input name="firm_name' + counter + '" type="text" id="firm_name' + counter + '" size="40"/></td></tr>'); 
    counter++; 
    return true; 
} 
+0

新添加的行在表單重新加載時尚不可見,如果出現錯誤 – raj

1

如果你想新添加的行,即使重裝後的形式繼續存在,那麼你需要它,否則每次保存到數據庫中的頁面重新加載的數據將會丟失。

+0

它是一個提交表單,所以如果點擊提交按鈕後表單發生某些錯誤,我希望新添加的行保留 – raj