lundi 6 juillet 2020

add :checked rule to jquery object variable

I'm trying to get the values of all inputs in my form one by one. These can be text inputs, radiobuttons or checkboxes. But I cannot get the radiobuttons or checkboxes values, it always return the same. I do this in a loop:

let input = $(div).find('input')
let name = input.attr("name")
if (input.attr("type") !== "radio" && input.attr("type") !== "checkbox"){
  console.log(input.val())
}
else {
  if (input.is(":checked")){
    console.log(input.val())
  }
}

But it checkes if any choice of input is checked and always returns the first option (even if "No" is checked, the input.val() is "Yes").

How to do that correctly? I know that the selector should look like $('.input:checked') but I don't know how to add :checked attribut to my input variable.




Aucun commentaire:

Enregistrer un commentaire