简体   繁体   English

提交前如何填写所有输入字段

[英]how to make all input fields to be filled before submitting

I want to make all input field to be filled before the user submit it. 我想在用户提交之前填写所有输入字段。 I tried to disable the submit button until my input fields have been filled in. I also tried to validate on client side but could not make it work, the code passes but the button is still not enabled even when all fields are filled out. 我尝试禁用提交按钮,直到填写完我的输入字段为止。我还尝试在客户端进行验证,但无法使其正常工作,代码通过,但是即使所有字段都已填写,该按钮仍未启用。 The button continues to be disabled for some reason. 由于某些原因,该按钮继续被禁用。

My _form.html.haml looks like: 我的_form.html.haml看起来像:

= simple_form_for @post, html: { multipart: true } do |f|
    - if @post.errors.any?
        #errors
            %h2
                = pluralize(@post.errors.count, "error")
                prevented this Post from saving
            %ul
                - @post.errors.full_messages.each do |msg|
                    %li= msg

    .form-group
        = f.input :title,:label => "Project Name", input_html: { class: 'form-control', :type => "text", :required => "required", :autofocus => true}
        %small#titleHelp.form-text.text-muted 

    .form-group
        = f.input :image,:label => "Image", input_html: { class: 'form-group',"aria-describedby" => "fileHelp", :required => "required", :type => "file",:autofocus => true }

    .form-group
        = f.input :link,:label => "Project Link", input_html: { class: 'form-control', :type => "url", :value => "https://", :required => "required" , :autofocus => true, id:'url-input'}


    .form-group
        = f.input :description,:label => "Description", input_html: { class: 'form-control', :rows => "3", :required => "required" , :autofocus => true, id: 'description'}
        %small#descriptionHelp.form-text.text-muted 


    %button.btn.btn-info{:type => "submit", :disabled => "disabled" } Add Project

I have tried this in application.js 我已经在application.js尝试过

$('#url-input, #description').on('blur keypress', function() {
  var urlInputLength = $('#url-input').val().length;
  var descriptionInputLength = $('#description').val().length;
  if (urlInputLength == 0 || descriptionInputLength == 0) {
    $('button').prop('disabled',true);
  } else {
    $('button').prop('disabled',false);
  }
});

I feel that my js is not connecting to my html code for some reason. 我觉得我的js由于某种原因未连接到我的html代码。 Also, I am wondering if there is any other way to make it work without JS. 另外,我想知道是否还有其他方法可以使它在没有JS的情况下工作。

Thank you in advance! 先感谢您!

validates :attribute, presence: true for each one of the attributes that correspond with one of your fields in the form. 对于与表单中的字段之一相对应的每个validates :attribute, presence: true The button will still be able to be clicked without all fields filled in, but it will just redirect them to the same page until they fill in all the fields. 在不填写所有字段的情况下,仍然可以单击该按钮,但是它将仅将它们重定向到同一页面,直到它们填写所有字段。 You can add custom messages too so if they submit without filling the fields, it will tell them what they still need to fill in. Then they can only move on once all fields are filled out. 您也可以添加自定义消息,这样,如果他们提交而没有填写字段,它将告诉他们仍然需要填写什么。然后,只有填写完所有字段后,它们才能继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM