简体   繁体   English

如何从 javascript 中删除只读属性

[英]How can remove read-only attribute from javascript

I have a checkbox in a span tag and input number.我在跨度标签和输入数字中有一个复选框。 Initially the input number is read-only, When the checkbox is being ticked, remove the read only attribute of the Input number which is just close to checkbox.最初输入的数字是只读的,当勾选复选框时,删除靠近复选框的输入数字的只读属性。 Here is my code.这是我的代码。 Any help would be appreciated任何帮助,将不胜感激

<tr>
    <td>
    <span><input type="checkbox"  asp-for="@Model.attendanceLogList[i].IsNormalAttendance" onclick="SetHrs(this);" /></span>
    <input type="number" readonly="readonly" placeholder="0.00" style="@(columnstyle);width: 5em"
                                       class="form-control format-text emphrs" asp-for="@Model.attendanceLogList[i].NormalHrs"  />
    </td>
</tr>
    <script>
     function SetHrs(element) {
            var rowIndex = $(element).closest("tr").attr('id');      
           if ($(element).is(":checked")) {
    $(this).closest('tr').find('input[type=number]').removeAttr('readonly');   //  Not working
                
            }
        }
    </script>

I think you do more trouble than necessary to handle everything.我认为你做的麻烦比处理所有事情所需要的要多。

  1. Don't mix javascript and jQuery.不要混合 javascript 和 jQuery。 Try to keep everything as tidy as possible, creating sort of controllers for your page blocks.尽量保持一切整洁,为您的页面块创建某种控制器。 For example here you could have a table controller例如在这里你可以有一个表 controller
  2. It's a bit ugly trying to find elements by id, especially when using elements you expect to be repeated (such as rows).尝试通过 id 查找元素有点难看,尤其是在使用您希望重复的元素时(例如行)。 If you're sure that the input follows the span , you can use jQuery's next to get that element.如果您确定输入遵循span ,则可以使用 jQuery 的next来获取该元素。

So in the following example what I did was:所以在下面的例子中,我所做的是:

  1. Attaching the event listener using event delegation (removed the onclick attribute from the checkbox)使用事件委托附加事件侦听器(从复选框中删除了 onclick 属性)
  2. Getting the event target from the event argument从事件参数中获取事件目标
  3. Finding the readonly input, using the suggestion above使用上面的建议查找只读输入
  4. Removing the readonly attribute, only when the checkbox is checked, so I did a little improvement, if you turn off the checkbox the readonly will be added again.删除只读属性,只有在复选框被选中时,所以我做了一点改进,如果你关闭复选框,只读将再次添加。

 jQuery(function($) { $("table").on("change", "[name='foo']", function(e){ const $target = $(e.target); const $targetInput = $target.parent().next(); $targetInput[$target.is(":checked")? "removeAttr": "attr"]("readonly", "readonly"); }); });
 table { width: 100%; border: 1px solid #e2e2e2; } input:read-only { border: 1px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tbody> <tr> <td> <span> <input name="foo" type="checkbox" /> </span> <input type="number" readonly="readonly" placeholder="0.00" /> </td> </tr> </tbody> </table>

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

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