0

in update(So each text box will have value in it) i want by default Save Button to be disable & Enable it when one of the text box value is changed.using jQuery or Js.

@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })
@Html.TextBoxFor(model => model.LastName, new { @class = "form-control" })
@Html.TextBoxFor(model => model.Address, new { @class = "form-control" })
@Html.TextBoxFor(model => model.Phone_No, new { @class = "form-control" })

<input type="submit" class="btn-mvc btn-mvc-green btn-mvc-fullwidth" value="Save Record" id="saveRecord">

2 Answers 2

1

Make sure you add a reference to jQuery >= 1.9 then use the code below:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#saveRecord").prop('disabled', true);

        $("#saveRecord").click(function () {

        });

        $(".form-control").on("change keyup paste",function () {
            var text = $(this).val();
            var textLength = text.length;

            if(textLength > 0)
                $("#saveRecord").prop('disabled', false);
            else
                $("#saveRecord").prop('disabled', true);
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

copy and paste same value will enable saveRecord button :(
That's what you asked for.If text is entered into one of the text boxes the button should be enabled?
So then just change the code above to handle the required jQuery event
0

You can listen to keyup event for text boxes and remove disable attribute when any of them have value. Check this sample code:

$('input[type=text].form-control').on("change", function(){
  if($(this).val() != ''){
    $('#saveRecord').attr('disabled', false);
  }else{
     $('#saveRecord').attr('disabled', true);
  }
});

You have to add disabled="disabled" to the save button as following:

<input type="submit" class="btn-mvc btn-mvc-green btn-mvc-fullwidth" value="Save Record" id="saveRecord" disabled="disabled">

1 Comment

i'm sorry brother Mohammed your code is working fine..i forgot to add disabled...jazakallahu khairan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.