在使用rails中的jquery提交之前,将值从输入框复制到隐藏字段

时间:2013-09-23 13:13:08

标签: javascript ruby-on-rails ruby-on-rails-4

<%= simple_form_for @user, :url => create_performer_path do |f| %>
 <%= f.input :name, :label => "Model Name" %></p>
 <%= f.input :email %></p>
 <%= f.input :password %></p>  
 <%= f.input :password_confirmation, :id =>"password" %></p> 
 <%= f.hidden_field_tag :password_next ,:id=>"password_next"%>
 <p><%= f.submit "Save" ,:onclick=>"value"%></p>
<% end %>

这是我用来使用设计创建用户的rails表单。 现在这是我在同一页面中编写的脚本,用于将password_confirmation值复制到password_next值。

<script type="text/javascript">
 function value
 {
  document.form.password_next.value=document.form.password.value
  }
 <script>

但这不起作用。我做错了什么?

这是纯html输出     功能价值   {     document.form.password_next.value = document.form.password.value   } 创建表演者

<form accept-charset="UTF-8" action="/create_performer" class="simple_form new_user" enctype="multipart/form-data" id="new_user" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="dF4uCaq0ymIoc5ooypVt3ohYlD/4+95ho28hNuP6kfw=" /></div>
 <div class="control-group string optional user_name"><label class="string optional control-label" for="user_name">Model Name</label><div class="controls"><input class="string optional" id="user_name" name="user[name]" type="text" /></div></div></p>
 <div class="control-group email optional user_email"><label class="email optional control-label" for="user_email">Email</label><div class="controls"><input class="string email optional" id="user_email" name="user[email]" type="email" value="" /></div></div></p>
 <div class="control-group password optional user_password"><label class="password optional control-label" for="user_password">Password</label><div class="controls"><input class="password optional" id="user_password" name="user[password]" type="password" /></div></div></p>  
 <div class="control-group password optional user_password_confirmation"><label class="password optional control-label" for="user_password_confirmation">Password confirmation</label><div class="controls"><input class="password optional" id="user_password_confirmation" name="user[password_confirmation]" type="password" /></div></div></p>
  <div class="controls performer_check_box">
          <label class="string optional control-label" for="user_roles">Roles</label>
            <input checked="checked" id="user_role_ids_" name="user[role_ids][]" type="checkbox" value="2" />
            performer<br />
  </div>

<div class="control-group string optional user_performer_first_name"><label class="string optional control-label" for="user_performer_attributes_first_name">User name</label><div class="controls">
<input class="string optional" id="user_performer_attributes_first_name" name="user[performer_attributes][first_name]" type="text" /></div></div></p>
  <label class="string optional control-label" for="user_performer_attributes_location">Location</label>
  <select id="user_performer_attributes_location_id" name="user[performer_attributes][location_id]"><option value="">Location</option>
<option value="1">US</option>
<option value="2">Canada</option>
<option value="3">UK</option></select>
<div class="control-group file optional user_performer_avatar"><label class="file optional control-label" for="user_performer_attributes_avatar">Avatar</label><div class="controls"><input class="file optional" id="user_performer_attributes_avatar" name="user[performer_attributes][avatar]" type="file" /></div></div></p>
<div class="control-group file optional user_performer_photo_id"><label class="file optional control-label" for="user_performer_attributes_photo_id">Photo</label><div class="controls"><input class="file optional" id="user_performer_attributes_photo_id" name="user[performer_attributes][photo_id]" type="file" /></div></div></p>
<div class="control-group file optional user_performer_profile_thumb"><label class="file optional control-label" for="user_performer_attributes_profile_thumb">Profile thumb</label><div class="controls"><input class="file optional" id="user_performer_attributes_profile_thumb" name="user[performer_attributes][profile_thumb]" type="file" /></div></div></p>
<div class="control-group file optional user_performer_profile_gif"><label class="file optional control-label" for="user_performer_attributes_profile_gif">Profile gif</label><div class="controls"><input class="file optional" id="user_performer_attributes_profile_gif" name="user[performer_attributes][profile_gif]" type="file" /></div></div>
 <select id="user_performer_attributes_white_label_id" name="user[performer_attributes][white_label_id]"><option value="">Owner</option>
<option value="1">master</option>
<option value="2">dio888</option>
<option value="3">avm_studio</option></select>
<input id="password_next" name="user[password_next]" type="hidden" />
<p><input name="commit" onhover="value" type="submit" value="Save" /></p>
</form>

1 个答案:

答案 0 :(得分:0)

脚本:

<script type="text/javascript">
 function copy_pwd_confirmation()
 {
  document.form.password_next.value=document.form.password.value;
  }
 <script>

在pwd确认中调用此方法onblur,

 <%= f.input :password_confirmation, :id =>"password" ,  onblur: "copy_pwd_confirmation()" %></p> 

编辑:

你可以尝试jquery insted javascript ..

  $("#password_next").value($("#user_password_confirmation").value());