Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
jQuery: determine si el elemento de entrada es un cuadro de texto o una lista de selección

¿Cómo determinaría si el elemento devuelto por un filtro de entrada en jQuery es un cuadro de texto o una lista de selección?

Quiero tener un comportamiento diferente para cada uno (el cuadro de texto devuelve el valor del texto, la selección devuelve tanto la clave como el texto)

Configuración de ejemplo:

 <div id="InputBody"> <div class="box"> <span id="StartDate"> <input type="text" id="control1"> </span> <span id="Result"> <input type="text" id="control2"> </span> <span id="SelectList"> <select> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> </span> </div> <div class="box"> <span id="StartDate"> <input type="text" id="control1"> </span> <span id="Result"> <input type="text" id="control2"> </span> <span id="SelectList"> <select> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> </span> </div>

y luego el guion:

 $('#InputBody') // find all div containers with class = "box" .find('.box') .each(function () { console.log("child: " + this.id); // find all spans within the div who have an id attribute set (represents controls we want to capture) $(this).find('span[id]') .each(function () { console.log("span: " + this.id); var ctrl = $(this).find(':input:visible:first'); console.log(this.id + " = " + ctrl.val()); console.log(this.id + " SelectedText = " + ctrl.find(':selected').text()); });
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Podrías hacer esto:

 if( ctrl[0].nodeName.toLowerCase() === 'input' ) { // it was an input }

o esto, que es más lento, pero más corto y más limpio:

 if( ctrl.is('input') ) { // it was an input }

Si quieres ser más específico, puedes probar el tipo:

 if( ctrl.is('input:text') ) { // it was an input }
over 4 years ago · Santiago Trujillo Report

0

alternativamente, puede recuperar las propiedades DOM con .prop

aquí hay un código de muestra para el cuadro de selección

 if( ctrl.prop('type') == 'select-one' ) { // for single select } if( ctrl.prop('type') == 'select-multiple' ) { // for multi select }

para cuadro de texto

 if( ctrl.prop('type') == 'text' ) { // for text box }
over 4 years ago · Santiago Trujillo Report

0

Si solo desea verificar el tipo, puede usar la función .is() de jQuery,

Como en mi caso, usé a continuación,

 if($("#id").is("select")) { alert('Select'); else if($("#id").is("input")) { alert("input"); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!