如何禁用提交表单按钮给定的特定条件

本文关键字:特定条件 按钮 何禁用 提交 表单 | 更新日期: 2023-09-27 18:12:52

我想禁用一个取消按钮,如果记录字段"状态"已被记录为取消。我已经知道如何禁用按钮,但问题是如何将jquery知道记录字段"状态"被取消。这里是代码

 @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>

任何帮助将不胜感激,谢谢:)

如何禁用提交表单按钮给定的特定条件

如果你知道状态被取消,你可以在Razor本身中禁用它。

    <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>

如果你想用jquery的方式:-

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

或者在函数内部你可以这样做:-

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

如果我没理解错的话,应该是这样的:

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