jQuery removeAttr,prop,attr不适用于必需'财产

发布于 2025-02-11 02:59:00 字数 1959 浏览 0 评论 0原文

美好的一天,我正在与遇到困难和解决与HTML形式组件的“必需”属性有关的所有问题的每个人接触。就我而言,每当我尝试使用removeAttr,attr或prop方法修改或设置它时,“必需”属性似乎不会更新。

注意:“占位持有人”属性的拆卸tr起作用,但是对于“必需”而言,它却没有。

$(document).ready(function() {
  $("select#decision").on('change', function() {
    var decValue = $("select#decision").val();

    if (decValue == 'For STR') {
      $("textarea#addtl_reason").prop('required', true);
      $("textarea#add_notes").removeAttr('placeholder');
      $("#sos").slideDown();
      $("#reason").slideDown();
      $("#narrative").slideDown();
    } 
    else {
      $("textarea#addtl_reason").removeAttr('required');
      $("#sos").slideUp();
      $("#reason").slideUp();
      $("#narrative").slideUp();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id = "narrative" class="form-group col-md-6">
  <label class="control-label col-md-5" for="addtl_reason">STR Narrative</label>
  
  <div class="col-md-7">
    <textarea cam-variable-name="addtl_reason"
              cam-variable-type="String"
              id="addtl_reason"
              maxlength="4000"
              class="form-control glow"
              rows="5"
              aria-describedby="notesHelpBlock"
              placeholder="Write your case narrative here..."
              onkeyup="cnt_reason_character()"
              autofocus></textarea>

    <small id="notesHelpBlock" class="form-text pull-right text-muted"> <span id="words_count" class="d-none form-text text-muted"> <span id="reason_textcount">0</span> / 4000</span></small>
    <small id="notesHelpBlock" class="form-text text-muted">Provide your case narrative here.</small>
  </div>
</div>

Good day, I am reaching out to everyone who has encountered difficulties and resolved issues related to the 'required' property of html form components. In my case, the 'required' property doesn't seem to update whenever I try to modify or set it using removeAttr, attr or prop methods.

Note: the removeAttr for the 'placeholder' property works, however for 'required' it doesn't.

$(document).ready(function() {
  $("select#decision").on('change', function() {
    var decValue = $("select#decision").val();

    if (decValue == 'For STR') {
      $("textarea#addtl_reason").prop('required', true);
      $("textarea#add_notes").removeAttr('placeholder');
      $("#sos").slideDown();
      $("#reason").slideDown();
      $("#narrative").slideDown();
    } 
    else {
      $("textarea#addtl_reason").removeAttr('required');
      $("#sos").slideUp();
      $("#reason").slideUp();
      $("#narrative").slideUp();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id = "narrative" class="form-group col-md-6">
  <label class="control-label col-md-5" for="addtl_reason">STR Narrative</label>
  
  <div class="col-md-7">
    <textarea cam-variable-name="addtl_reason"
              cam-variable-type="String"
              id="addtl_reason"
              maxlength="4000"
              class="form-control glow"
              rows="5"
              aria-describedby="notesHelpBlock"
              placeholder="Write your case narrative here..."
              onkeyup="cnt_reason_character()"
              autofocus></textarea>

    <small id="notesHelpBlock" class="form-text pull-right text-muted"> <span id="words_count" class="d-none form-text text-muted"> <span id="reason_textcount">0</span> / 4000</span></small>
    <small id="notesHelpBlock" class="form-text text-muted">Provide your case narrative here.</small>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2025-02-18 02:59:00

尝试Vanilla JavaScript:

.setAttribute('required', true/false);
.removeAttribute('required');
.toggleAttribute('required');

如果您在jQuery对象上使用Vanilla JavaScript方法,它将无法识别它。因此,您要么使用JavaScript方法引用元素(推荐)或退参考jQuery对象:

// Two ways to de-referencing a jQuery object
$('input')[0]
//OR
$('input').get(0)

零表示您仅针对第一个目标,因此,如果您已经知道这是第二个输入使用索引1。

// Using no ids, classes, or tagnames just indexes
document.forms[0].elements[1].onclick = e => 
  e.target.previousElementSibling.toggleAttribute('required');
  
// De-referencing the second .data 
$('.data')[1].setAttribute('required', true);
[required] {
  outline: 3px dashed red
}
<form>
  <input name='data' class='data'><input type='button' value='Toggle'><br>
  <input name='data' class='data'><br>
  <button>Submit</button>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

只是一些建议,您要删除的属性通常不会因为其性质而被删除。 占位符永远不需要删除,因为当用户专注于表单控件时,它会消失。 必需的不应删除,因为它有助于验证。您为什么需要删除它们?

Try vanilla JavaScript:

.setAttribute('required', true/false);
.removeAttribute('required');
.toggleAttribute('required');

If you use vanilla JavaScript methods on a jQuery object, it will not recognize it. So you either use JavaScript methods to reference an element (recommended) or de-reference the jQuery object:

// Two ways to de-referencing a jQuery object
$('input')[0]
//OR
$('input').get(0)

The zero indicates that you are targeting the first one only so if you already know that it's the second input use index 1.

// Using no ids, classes, or tagnames just indexes
document.forms[0].elements[1].onclick = e => 
  e.target.previousElementSibling.toggleAttribute('required');
  
// De-referencing the second .data 
$('.data')[1].setAttribute('required', true);
[required] {
  outline: 3px dashed red
}
<form>
  <input name='data' class='data'><input type='button' value='Toggle'><br>
  <input name='data' class='data'><br>
  <button>Submit</button>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Just some advice, the attributes you are removing are not commonly removed because of their nature. A placeholder never needs to be removed because it disappears when the user focuses on a form control. required shouldn't be removed because it facilitates validation. Why would you need to remove them?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文