lou lising
lou lising

Reputation: 71

How to disable a submit form button given a certain condition

I would like to disable a cancel button if the the record field "Status" is already recorded as cancelled. I already know how to disable a button but the problem is how would the jquery know that the record field "Status" is Cancelled. Here Are the Codes

 @foreach (var rDetail in Model.Customers.ToList()) { 

      <tr>
    <td>
        @Html.DisplayFor(model => rDetail.DateEntry)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateStart)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateEnd)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.Status.Name)
    </td>

    <td>
        @Html.DisplayFor(model => rDetail.UserCode)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateModified)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.Remarks)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", "RoomReservation", new { id = rDetail.Id}, null) |
        @using (Html.BeginForm("CancelReservation", "Rooms", new { roomId = Model.Id, reservationId = rDetail.Id, methodId = 0})) {
        <input type="submit" value="Cancel" class ="cancelSubmit"/>
        }
    </td>
</tr>

Any help will be appreciated, thanks :)

Upvotes: 1

Views: 779

Answers (2)

Matt Berkowitz
Matt Berkowitz

Reputation: 975

If I understand you, something like this should work:

$('#Status_Name').on('keypress', function(e) { //I think that's how the XFor handlers format ids
    $('button_to_disable').prop('disabled', this.value === 'Cancelled');
});

Upvotes: 0

PSL
PSL

Reputation: 123739

If you know the status is cancelled you can disable it in the Razor itself.

    <td>
        @Html.ActionLink("Details", "Details", "RoomReservation", new { id = rDetail.Id}, null);

  @if(rDetail.Status.Name.Equals("cancelled"))
  {
    <input type="submit" value="Cancel" class ="cancelSubmit" disabled/>
  }
  else
  {
        @using (Html.BeginForm("CancelReservation", "Rooms", new { roomId = Model.Id, reservationId = rDetail.Id, methodId = 0})) {
        <input type="submit" value="Cancel" class ="cancelSubmit"/>
        }
   }
    </td>

If you want to do it jquery way:-

$(function(){
$('.cancelSubmit').each(function(){
   if($(this).closest('tr').find('#Status_Name').text() === 'cancelled')
   {
       $(this).prop('disabled',true);
   }
  });
 });

or inside the function you could do :-

$(this).prop('disabled',
        $(this).closest('tr')
       .find('#Status_Name')
       .text() === 'cancelled');

Upvotes: 1

Related Questions