2014-03-27 48 views
0

商店註冊用戶獲得一些證書後,我得到用戶的教育手段名度和持續時間FROM_YEAR和TO_YEAR到靜態控件下方則是靜態控件代碼:動態控件的數據不在數據庫中

<table width="100%"> 
     <tr style = "text-align:center;"> 
     <td> 
      <%= f.text_field :tf_Degree,placeholder: "Degree" %> 
     </td> 
     </tr> 
     <tr style = "text-align:center;"> 
     <td> 
      From 
      <%= f.select :fromyear, (1995..Time.now.year).to_a, :include_blank => {:year => "Select year"} %> 
      to 
      <%= f.select :toyear, (1995..Time.now.year).to_a, :include_blank => {:year => "Select year"} %> 
     </td> 
     </tr> 
</table> 

而且如果我希望在add按鈕進一步進入更多的教育用戶,然後點擊我用下面的jQuery是動態控件代碼中添加動態控件:

<table> 
     <tr> 
     <td align="center"> 
      <table id="Controls"> 
      </table> 

      <div><input value="Add" type="submit" /></div> 
      <input id="valEdu" type="hidden" value="0" /> 

     </td> 
     </tr> 
</table> 

及以下用於創建動態控制的jQuery:

 <script type="text/javascript"> 

    $(document).ready(function() { 
     $("input[value='Add']").click(function(e) { 

      e.preventDefault(); 
      var hidval = $('#valEdu').val(); 
      if(hidval = 0) 
      { 
      hidval = 1; 
      } 
      else 
      { 
      hidval++; 
      } 
      alert(hidval); 

      var rownum=$("#Controls > tbody > tr").length; 
      alert(rownum); 
      var updated_row_num; 
      if (rownum == 0) 
      { 
       updated_row_num=0; 
      } 
      else { 
       updated_row_num=rownum/2; 
      } 

      // var field = $("#field").val(); 
      var year = new Date().getFullYear(); 

      var DDL_fromProfession = "<select name='ParametersFromSch["+ updated_row_num +"]' id='DDL_FromSchYear'>"; 
      for (var i = year; i >= 1950; --i) { 
       DDL_fromProfession += "<option text='" + i + "' value='" + i + "'>" + i + "</option>"; 
      } 
      DDL_fromProfession += "</select>"; 

       var DDL_ToProfession = "<select name='ParametersToSch["+ updated_row_num +"]' id='DDL_ToSchYear'>"; 
       for (var j = year; j >= 1950; --j) { 
        if (j != year) { 
         DDL_ToProfession += "<option text='" + j + "' value='" + j + "'>" + j + "</option>"; 
        } 
        else { 
         DDL_ToProfession += "<option text='Present' value='Present'>Present</option>"; 

        } 
       } 
       DDL_ToProfession += "</select>"; 

    var input1 = "<input name='parametersSch["+ updated_row_num +"]' id='field' type='text' placeholder='Degree' style='text-align:center;' onfocus='WaterMarkSchool(this, event);' onblur='WaterMarkSchool(this, event);' />" 
       var newRow = "<tr><td align='center' style='font-size: x-large; color: #212121;' height='35px'>" 
       + input1 + "</td></tr>"; 


var controls = "<tr><td>From "+ DDL_fromProfession + " To "+DDL_ToProfession+ "</td></tr>"; 
controls += "<br/><button type='button' class='btn_rmv'>Remove</button></td></tr>"; 

    $('#Controls').append(newRow); 
    $('#Controls').append(controls); 
    return false; 
    }); 

      $('#Controls').on('click', '.btn_rmv', function() { 
       alert("ali"); 
       var index = $(this).closest('tr').index() + 2 
       $('#Controls tr:nth-child(n+' + (index - 2) + ')').remove(); 
       return false; 
      }); 

}); 
</script> 

但問題是,只有靜態的控制數據存儲在數據庫和動態控件不以dB爲單位存儲,爲解決這個問題,我想補充隱藏的控制和jQuery的我檢查,如果隱藏控制值等於零,則其保存1,否則其increament象下面這樣:

var hidval = $('#valEdu').val(); 
       if(hidval = 0) 
       { 
       hidval = 1; 
       } 
       else 
       { 
       hidval++; 
       } 
       alert(hidval); 

而在控制器頁我檢查隱藏控件的條件:

{用於存儲靜態數據)

if !params[:Educations][:tf_Degree].blank? && params[:Educations][:tf_Degree] != "Degree" 
      @Degree = params[:Educations][:tf_Degree] 
      @From = params[:Educations][:fromyear] 
      @To = params[:Educations][:toyear] 
      @Education = Education.new(:UserID => current_user.id, :SchoolName => @Degree, :SchoolFrom => @From, :SchoolTo => @To) 
      @Education.save 
     end 
     {for storing dynamic data} 
     if params[:valEdu].present? && params[:valEdu] > 0 {here I am checking hidden value} 
      params[:parametersSch].each do |keydeg, degree| 
      if !degree.nil? 
       @degree=degree 
       puts @degree 
       params[:ParametersFromSch].each do |keyfrom, from| 
       if keydeg== keyfrom 
        @from=from 
       end 
      end 
      params[:ParametersToSch].each do |keyto, to| 
       if keydeg==keyto 
        @to=to   
       end 
      end 
      @Education = Education.new(:UserID => current_user.id, :SchoolName => @degree, :SchoolFrom => @from, :SchoolTo => @to) 
      @Education.save 
      end 
      end 
     end 
     #render 'index' 
     redirect_to '/default' 
     end 

在上面我正在檢查它隱藏的控制值是0,然後它將數據存儲在分貝,但它不工作,請幫助我,等待您的答覆。 謝謝

+0

希望這會有所幫助。 http://railscasts.com/episodes/74-complex-forms-part-2。 – ejo

回答

0

好吧,我真的不明白一切,但我注意到你的代碼中至少有2個錯誤。

首先,在你的JavaScript代碼

if(hidval = 0){ 
    ... 
} 
else{ 
    ... 
} 

在這一段代碼,我認爲hidval爲開頭的字符串,所以它不是0'0'。現在,hidval = 0是一個總是返回false的狀態!你想寫的是hidval == 0我想。另外,我不明白這個情況,你需要什麼,因爲最後你總是做a + 1不是嗎?因此,而不是整個條件,爲什麼不寫: hidval++。它會有相同的行爲。

最後,在你的Ruby代碼:

params[:valEdu] > 0 

可能會提高類似的錯誤:ArgumentError: comparison of String with 0 failed

因爲查詢參數總是字符串,也許你可以做

params[:valEdu].to_i > 0 

坎,如果你想更新隱藏輸入的值,這不是你怎麼做的,在這裏你只是更新了javascript變量的值,但沒有觸及DOM元素,你可以爲寫的是:

a = $('#valEdu').val(); 
$('#valEdu').val(++a); 

我不知道這是否是一個答案,但至少它會幫助你走得更遠我猜!