商店註冊用戶獲得一些證書後,我得到用戶的教育手段名度和持續時間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,然後它將數據存儲在分貝,但它不工作,請幫助我,等待您的答覆。 謝謝
希望這會有所幫助。 http://railscasts.com/episodes/74-complex-forms-part-2。 – ejo