简体   繁体   English

不使用 Jquery 覆盖字段集禁用属性(仅在 HTML 中)

[英]Override fieldset disabled attribute without using Jquery(Only in HTML)

I have a complex form with many <input> elements, most of which I need to disable conditionally (via AngularJS to precise, but this question primarily targets the HTML aspect).我有一个包含许多<input>元素的复杂表单,其中大部分我需要有条件地禁用(通过 AngularJS 进行精确,但这个问题主要针对 HTML 方面)。

In order to avoid having to set a disabled attribute on every element, I place my inputs into a <fieldset> and set the disabled attribute once.为了避免必须在每个元素上设置禁用属性,我将输入放入<fieldset>并设置禁用属性一次。 This disables all contained inputs fields.这将禁用所有包含的输入字段。

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Fieldsets</title>
</head>

<body>
  <form>
    <fieldset disabled="true">

      <input type="text" placeholder="first">
      <input type="text" placeholder="second">

      <!-- many, many more -->

      <!-- trying to override here -->
      <input type="text" placeholder="last" disabled="false">

    </fieldset>
  </form>
</body>

</html>

Now I need to make a few exceptions and keep some inputs enabled even though the surrounding <fieldset> is disabled.现在我需要做一些例外并保持一些输入启用,即使周围的<fieldset>被禁用。 I tried overriding the attribute there by setting disabled="false" , but this does not work.我尝试通过设置disabled="false"覆盖那里的属性,但这不起作用。

Is there any elegant way to override the disabled attribute?有没有什么优雅的方法来覆盖 disabled 属性?

$.each($("fieldset[disabled='true']"),function(index,value){
    $(value).removeAttr("disabled");
  $.each($(value).find("input"),function(index,value) {
    if($(value).attr("disabled")){
        $(value).removeAttr("disabled");
    } else {
        $(value).attr("disabled","true");
    }
  })
});

I used Jquery for this solution.我在这个解决方案中使用了 Jquery。 i found each fieldset which is set disabled and removed that disabled attribute.我发现每个字段集都设置为禁用并删除了禁用属性。 Then for each fieldsets that are found, i found the input elements in each fieldset and for each input field that has disabled attribute, i have removed the attribute(disabled).然后对于找到的每个字段集,我在每个字段集中找到了输入元素,对于每个具有禁用属性的输入字段,我已经删除了属性(禁用)。 Also i added disabled attribute to the other input fields which are not set with disabled=false attribute.我还向其他未使用 disabled=false 属性设置的输入字段添加了 disabled 属性。

Also you can check the api for angularjs : https://docs.angularjs.org/api/ng/function/angular.element您也可以检查 angularjs 的 api: https ://docs.angularjs.org/api/ng/function/angular.element

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

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