jQuery disable/enable submit button

后端 未结 21 3098
难免孤独
难免孤独 2020-11-22 07:30

I have this HTML:



How can I do something li

相关标签:
21条回答
  • 2020-11-22 07:52

    Here's the solution for file input field.

    To disable a submit button for file field when a file is not chosen, then enable after the user chooses a file to upload:

    $(document).ready(function(){
        $("#submitButtonId").attr("disabled", "disabled");
        $("#fileFieldId").change(function(){
            $("#submitButtonId").removeAttr("disabled");
        });
    });
    

    Html:

    <%= form_tag your_method_path, :multipart => true do %><%= file_field_tag :file, :accept => "text/csv", :id => "fileFieldId" %><%= submit_tag "Upload", :id => "submitButtonId" %><% end %>
    
    0 讨论(0)
  • 2020-11-22 07:53
    $(function() {
      $(":text").keypress(check_submit).each(function() {
        check_submit();
      });
    });
    
    function check_submit() {
      if ($(this).val().length == 0) {
        $(":submit").attr("disabled", true);
      } else {
        $(":submit").removeAttr("disabled");
      }
    }
    
    0 讨论(0)
  • 2020-11-22 07:53

    If the button is itself a jQuery styled button (with .button()) you will need to refresh the state of the button so that the correct classes are added / removed once you have removed/added the disabled attribute.

    $( ".selector" ).button( "refresh" );
    
    0 讨论(0)
  • 2020-11-22 07:53

    Please see the below code to enable or disable Submit button

    If Name and City fields has value then only Submit button will be enabled.

    <script>
      $(document).ready(function() {
        $(':input[type="submit"]').prop('disabled', true);
    
        $('#Name').keyup(function() {
          ToggleButton();
        });
        $('#City').keyup(function() {
          ToggleButton();
        });
    
      });
    
    function ToggleButton() {
      if (($('#Name').val() != '') && ($('#City').val() != '')) {
        $(':input[type="submit"]').prop('disabled', false);
        return true;
      } else {
        $(':input[type="submit"]').prop('disabled', true);
        return false;
      }
    } </script>
    <form method="post">
    
      <div class="row">
        <div class="col-md-4">
          <h2>Getting started</h2>
          <fieldset>
            <label class="control-label text-danger">Name</label>
            <input type="text" id="Name" name="Name" class="form-control" />
            <label class="control-label">Address</label>
            <input type="text" id="Address" name="Address" class="form-control" />
            <label class="control-label text-danger">City</label>
            <input type="text" id="City" name="City" class="form-control" />
            <label class="control-label">Pin</label>
            <input type="text" id="Pin" name="Pin" class="form-control" />
            <input type="submit" value="send" class="btn btn-success" />
          </fieldset>
        </div>
      </div>
    </form>

    0 讨论(0)
  • 2020-11-22 07:54

    Vanilla JS Solution. It works for a whole form not only one input.

    In question selected JavaScript tag.

    HTML Form:

    var form = document.querySelector('form')
        var inputs = form.querySelectorAll('input')
        var required_inputs = form.querySelectorAll('input[required]')
        var register = document.querySelector('input[type="submit"]')
        form.addEventListener('keyup', function(e) {
            var disabled = false
            inputs.forEach(function(input, index) {
                if (input.value === '' || !input.value.replace(/\s/g, '').length) {
                    disabled = true
                }
            })
            if (disabled) {
                register.setAttribute('disabled', 'disabled')
            } else {
                register.removeAttribute('disabled')
            }
        })
    <form action="/signup">
            <div>
                <label for="username">User Name</label>
                <input type="text" name="username" required/>
            </div>
            <div>
                <label for="password">Password</label>
                <input type="password" name="password" />
            </div>
            <div>
                <label for="r_password">Retype Password</label>
                <input type="password" name="r_password" />
            </div>
            <div>
                <label for="email">Email</label>
                <input type="text" name="email" />
            </div>
            <input type="submit" value="Signup" disabled="disabled" />
        </form>

    Some explanation:

    In this code we add keyup event on html form and on every keypress check all input fields. If at least one input field we have are empty or contains only space characters then we assign the true value to disabled variable and disable submit button.

    If you need to disable submit button until all required input fields are filled in - replace:

    inputs.forEach(function(input, index) {
    

    with:

    required_inputs.forEach(function(input, index) {
    

    where required_inputs is already declared array containing only required input fields.

    0 讨论(0)
  • 2020-11-22 07:55

    take look at this snippet from my project

     $("input[type="submit"]", "#letter-form").on("click",
            function(e) {
                 e.preventDefault();
    
    
    $.post($("#letter-form").attr('action'), $("#letter-form").serialize(),
                     function(response) {// your response from form submit
                        if (response.result === 'Redirect') {
                            window.location = response.url;
                        } else {
                            Message(response.saveChangesResult, response.operation, response.data);
                        }
    });
    $(this).attr('disabled', 'disabled'); //this is what you want
    

    so just disabled the button after your operation executed

    $(this).attr('disabled', 'disabled');

    0 讨论(0)
提交回复
热议问题