简体   繁体   English

使用jQuery启用/禁用MVC控件

[英]Enabling/Disabling MVC controls with jQuery

I've got a table with multiple editable fields that correspond to an MVC model object. 我有一个表,其中包含多个与MVC模型对象相对应的可编辑字段。 Currently, I've got a cell in each row with two buttons that will toggle between an edit/save function when they are clicked. 当前,我在每行中都有一个带有两个按钮的单元格,当单击它们时,它们将在编辑/保存功能之间切换。 I've got the buttons working now to where it will switch between the two buttons so that only one is visible at a time. 我现在已经将这些按钮工作到可以在两个按钮之间切换的位置,以便一次只能看到一个。

The Problem: 问题:

Currently, I'm setting a disabled property in Razor that successfully grays out the controls when they are not in 'edit' mode. 当前,我正在Razor中设置一个禁用的属性,当这些控件不在“编辑”模式时,它们可以成功地使控件变灰。

<table>
     <thead>
          ...stuff here
     </thead>
     <tbody>
@foreach(var item in Model)
{
  <tr>
     ...some columns
     <td class="selectDropDown">
          @Html.DropDownListFor(x => item.Prop1, (SelectList)ViewBag.Prop1Options, item.Prop1, new { @class = "form-control", @disabled=true })
     </td>
     <td class="editableTxt">
          @Html.TextBoxFor(x => item.Prop2, new { @class = "form-control", @disabled = "true"})
     </td>
     <td class="checkBox">
        @Html.CheckBoxFor(x => item.Prop3, new { @class = "checkbox", @disabled = "true" })
          ...Other Columns
      <td class="buttons">
         <btn class="btn btn-sm btn-primary editBtn"><i class="glyphicon glyphicon-edit"></i></btn>
         <btn class="btn btn-sm btn-success saveBtn" style="display:none"><i class="glyphicon glyphicon-floppy-disk"></i></btn>
       </td>
      </tr>
}
   </tbody>
</table>

So now in jQuery, I've got two handlers which should simply do the following when clicked: 所以现在在jQuery中,我有两个处理程序,当单击它们时,它们应该简单地执行以下操作:

  1. Hide the button not currently in use 隐藏当前不使用的按钮
  2. Display the other function button 显示其他功能按钮
  3. Enable/Disable the editable fields. 启用/禁用可编辑字段。

Here is the code: 这是代码:

 $(document).on('click', '.editBtn', function () {


    var editButton = $(this);
    var selectedRow = $(this).parent();

    selectDropDown = selectedRow.closest('tr').children('td.selectDropDown');
    editableText = selectedRow.closest('tr').children('td.editableTxt');
    checkBox = selectedRow.closest('tr').children('td.checkBox');


    selectDropDown.prop('disabled', false);
    editableText.prop('disabled', false);
    checkBox.prop('disabled', false);

    editButton.hide();
    selectedRow.find('.saveBtn').show();
});

One of these functions exists for both buttons and they are successful at showing/hiding the buttons but the properties are not enabling/disabling on click as I'm expecting them to. 这两个按钮都具有这些功能之一,它们可以成功显示/隐藏按钮,但是属性并未像我期望的那样启用/禁用单击。

I have tried using $().attr() and $().removeAttr() for this as well. 我也尝试过使用$().attr()$().removeAttr() Additionally, I have changed the disabled property to 'disabled' , instead of the current Boolean value with no luck. 此外,我将disable属性更改为'disabled' ,而不是没有运气的当前布尔值。 Is there some reason this would not work in MVC? 是否由于某些原因在MVC中不起作用? Is there something going on in the background with my functions that would prevent the modification of the HTML properties? 我的函数在后台发生什么事情,从而阻止了HTML属性的修改?

EDIT: Should also note that: 编辑:还应注意:

selectDropDown = selectedRow.closest('tr').children('td.selectDropDown');

returns the following html: 返回以下html:

<select class="form-control" disabled="true" id="item_Prop" name="item.Prop"><option value="">Value</option>
    <option>Value</option>
    <option>Value</option>
    <option>Value</option>
    <option>Value</option>
    <option>Value</option>
    </select>

using jquery: 使用jQuery:

   @Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { class = "disabledme" })

JQuery: jQuery的:

$function(){
    $(".disabledme").click(function(){
        return false;
    }
}

Fixed. 固定。

The issue WAS with the selector. 选择器出现问题。

Additionally, all disabled controls were given the same class so they could be enabled/disabled all at once. 此外,所有禁用的控件都具有相同的类,因此可以一次启用/禁用它们。

Razor: 剃刀:

    <td>
     @Html.DropDownListFor(x => item.Prop1, (SelectList)ViewBag.Prop1Options, item.Prop1, new { @class = "form-control enableControl", @disabled="disabled" })
   </td>

jQuery: jQuery的:

  $(document).on('click', '.editBtn', function () {


    var editButton = $(this);
    var selectedRow = $(this).parent();


    disabledControls = selectedRow.closest('tr').find('.enableControl');


    disabledControls.prop('disabled', false);

    editButton.hide();
    selectedRow.find('.saveBtn').show();

});

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

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