gpt4 book ai didi

javascript - 无法启用禁用的按钮

转载 作者:行者123 更新时间:2023-11-28 02:41:02 25 4
gpt4 key购买 nike

我一直在尝试所有方法来启用禁用的元素,但我无法弄清楚为什么它不起作用。我尝试这样:

var item = document.getElementById('admin-item-select');
var edit = document.getElementById('admin-item-editbtn');
var del = document.getElementById('admin-item-delbtn');

item.addEventListener('change', function(e) {

if (this.value !== "") {
console.log("clicked");
edit.setAttribute("disabled", false);
console.log("edit enabled");
del.setAttribute("disabled", false);
console.log("del enabled");
}
});
  <select class="input-sm item-input" id="admin-item-select" name="itemSelect">
<option value="">Choose Item</option>
<option value="1">blabla</option>
<option value="2">bloblo</option>
</select>
<button href="http://google.com" id="admin-item-editbtn" disabled="true" >Edit Item</button>
<button href="http://amazon.com" id="admin-item-delbtn" type="submit" disabled="true">Delete Item</button>

我也尝试使用类和方法 toogleClass()..我试过 prop()

知道我做错了什么吗?

最佳答案

方案一(纯JS),使用edit.disabled = false;

var item = document.getElementById('admin-item-select');
var edit = document.getElementById('admin-item-editbtn');
var del = document.getElementById('admin-item-delbtn');

item.addEventListener('change', function(e) {
if (this.value !== "") {
console.log("clicked");
edit.disabled = false;
console.log("edit enabled");
del.disabled = false;
console.log("del enabled");
}
});
<select class="input-sm item-input" id="admin-item-select" name="itemSelect">
<option value="">Choose Item</option>
<option value="1">blabla</option>
<option value="2">bloblo</option>
</select>
<button href="http://google.com" id="admin-item-editbtn" disabled="true">Edit Item</button>
<button href="http://amazon.com" id="admin-item-delbtn" type="submit" disabled="true">Delete Item</button>

选项 2 (jQuery) $('#admin-item-editbtn').prop('disabled', false);

var item = document.getElementById('admin-item-select');

item.addEventListener('change', function(e) {
if (this.value !== "") {
console.log("clicked");
$('#admin-item-editbtn').prop('disabled', false);
console.log("edit enabled");
$('#admin-item-delbtn').prop('disabled', false);
console.log("del enabled");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="input-sm item-input" id="admin-item-select" name="itemSelect">
<option value="">Choose Item</option>
<option value="1">blabla</option>
<option value="2">bloblo</option>
</select>
<button href="http://google.com" id="admin-item-editbtn" disabled="true">Edit Item</button>
<button href="http://amazon.com" id="admin-item-delbtn" type="submit" disabled="true">Delete Item</button>

关于javascript - 无法启用禁用的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com