英文:
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 ($('div').hasClass('exceeded-size')) {
$('#btnSubmit').prop('disabled', true);
} else {
$('#btnSubmit').prop('disabled', false);
}
});
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案1
得分: 1
if ($('i').hasClass('exceeded-size')) {
$('#btnSubmit').prop('disabled', true);
} else {
$('#btnSubmit').prop('disabled', false);
}
英文:
if ($('i').hasClass('exceeded-size')) {
$('#btnSubmit').prop('disabled', true);
} else {
$('#btnSubmit').prop('disabled', false);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论