简体   繁体   中英

How do i set a button to disable with jQuery

More precisely, what is wrong with the following code, which makes it so that fields will disable int the Faculty button only if I've double-clicked the Department button first?

I've attached as a codepen for full context

$("#fac-button").on('click', function (e) {
    if($(".fName").is(":disabled")){$(".fName").prop("disabled", false)}
    if($(".lName").is(":disabled")){$(".lName").prop("disabled", false)}
    if($(".empId").is(":disabled")){$(".empId").prop("disabled", false)}
    if($(".headC").is(":enabled")) {$(".headC").prop("disabled", true)}
    if($(".startDate").is(":disabled")) {$(".startDate").prop("disabled", false)}
    if($(".stillEmployed").is(":disabled")) {$(".stillEmployed").prop("disabled", false)} 
    if($(".endDate").is(":disabled")) {$(".endDate").prop("disabled", false)}

Any help would be great, thanks

There is NEVER a need for an if when you have something that takes a boolean.

In your case just negate the boolean

$("#fac-button").on('click', function(e) {
  ["fname", "lName", "empId", "headC", "startDate", "stillEmployed", "endDate"]
  .forEach(sel => {
    const elem = document.querySelector("." + sel);
    elem.disabled = !elem.disabled;
  })
})

You can add

$(".headC").removeAttr("disabled")

if needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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