I want to disable button if element contains class using javascript

huangapple go评论64阅读模式
英文:

I want to disable button if element contains class using javascript

问题

我想要禁用按钮,如果元素包含类,并且我正在尝试这样做,但对我来说没有任何作用。 请帮帮我,谢谢

$(function() {
 if ($('div').hasClass('exceeded-size')) {
    $('#btnSubmit').prop('disabled', true);
 } else {
    $('#btnSubmit').prop('disabled', false);
 }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="image-container" data-image-index="0" id="mup-image-0" data-acceptable-image="0">
  <div class="image-size">3.1 MB</div>
  <div class="exceeded-size">greater than 1 MB</div>
  <img src="blob:http://127.0.0.1:8000/20bd149e-3368-4167-9b4b-a2719504a5ad" class="image-preview" alt="">
</div>
<div class="modal-footer">
  <button type="submit" id="btnSubmit" class="btn btn-primary w-100">Save</button>
</div>
英文:

I want to disable button if element contains class and I am trying to do it, but nothing works for me. please help me thanks

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
 if ($(&#39;div&#39;).hasClass(&#39;exceeded-size&#39;)) {
    $(&#39;#btnSubmit&#39;).prop(&#39;disabled&#39;, true);
 } else {
    $(&#39;#btnSubmit&#39;).prop(&#39;disabled&#39;, false);
 }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;image-container&quot; data-image-index=&quot;0&quot; id=&quot;mup-image-0&quot; data-acceptable-image=&quot;0&quot;&gt;
  &lt;div class=&quot;image-size&quot;&gt; 3.1 MB &lt;/div&gt;
  &lt;div class=&quot;exceeded-size&quot;&gt; greater than 1 MB &lt;/div&gt;
  &lt;img src=&quot;blob:http://127.0.0.1:8000/20bd149e-3368-4167-9b4b-a2719504a5ad&quot; class=&quot;image-preview&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;

<div class="modal-footer">
<button type="submit" id="btnSubmit" class="btn btn-primary w-100">Save</button>
</div>
<!-- end snippet -->

答案1

得分: 1

if ($('i').hasClass('exceeded-size')) {
  $('#btnSubmit').prop('disabled', true);
} else {
  $('#btnSubmit').prop('disabled', false);
}
英文:
if ($(&#39;i&#39;).hasClass(&#39;exceeded-size&#39;)) {
  $(&#39;#btnSubmit&#39;).prop(&#39;disabled&#39;, true);
} else {
  $(&#39;#btnSubmit&#39;).prop(&#39;disabled&#39;, false);
}

huangapple
  • 本文由 发表于 2023年2月19日 02:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定