2017-12-20 2 views
1

Ich mache ein System, wo nach Unschärfe eine HTML-Auswahl anhängen und Benutzer müssen eine Option aus diesem Auswahlfeld auswählen. Aber ich muss der ausgewählte Wert sollte automatisch wählen, wenn Benutzer drücken denselben Wert Schlüssel in der Tastatur, als ob i 2 drücken Sie den gewählten Wert 2 sein sollteWert auswählen als Tastatur-Taste drücken

$('input').on('blur', function() { 
 
    $(this).after("<select class=\"form-control\"><option value=\"Option\">option</option><option value=\"2\">2 </option><option value=\"3\">3 </option><option value=\"4\">4</option><option value=\"5\">5 </option><option value=\"6\">6 </option><option value=\"7\">7 </option><option value=\"8\">8 </option></select>"); 
 
    $('.form-control').on('change', function() { 
 
    $('input').after('<span>' + $(this).val() + '</span>'); 
 
    $(this).hide(); 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text">

+0

Ihre Frage ist nicht klar. Wie beim Snippet funktioniert es so, wie Sie es wollten. – 31piy

+0

@ 31piy danke für Ihre Antwort, es funktioniert, aber ich möchte nicht die Maus verwenden, um den Wert auszuwählen, drücken Sie einfach die gleiche Taste in der Tastatur es sollte automatisch diese Option auswählen und der Wert sollte nach der Eingabe – Codexx

Antwort

0

Einfach Einstellung des Fokus auf die neue Select-Steuerung wie durch das Autofokus Attribut zu Ihrem Select-Steuerelement sollte den Trick tun.

$('input').on('blur',function(){ 
 
$(this).after("<select class=\"form-control\" tabindex=\"1\" autofocus><option value=\"Option\">option</option><option value=\"2\">2 </option><option value=\"3\">3 </option><option value=\"4\">4</option><option value=\"5\">5 </option><option value=\"6\">6 </option><option value=\"7\">7 </option><option value=\"8\">8 </option></select>"); 
 
$('.form-control').on('change',function(){ 
 
$('input').after('<span>'+$(this).val()+'</span>'); 
 
$(this).hide(); 
 
$('.text-control').val($('input').val() + $(this).val()); 
 
}); 
 
$('.form-control').focus(); 
 
})
<script 
 
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<input type="text" class="text-control">

+0

ich habe anhängen updated your fiddle: https://jsfiddle.net/vsv5Lp5u/7/ –

+0

danke @tony mathew für die antwort, aber wie man auswahl wert, wenn ich gleiche wert-taste in meiner tastatur – Codexx

+0

@Codexx für mich es funktioniert! Ich tippe etwas in das Textfeld, klicke irgendwo anders oder drücke die Tabulatortaste, um die Unschärfe zu erzeugen, und der Fokus liegt auf der neuen Auswahl. Wenn du dann 3 drückst, wird der Wert in der Auswahl gesetzt. In Chrome und IE überprüft. Ist es das Textfeld, das Sie festlegen möchten? –

0

Wenn der Benutzer nur die Eingabe verschwimmen dann zeigen Sie nur die Auswahlbox

Dann, wenn es etwas Wert in der Eingabe ist, wird es in der Auswahlbox ausgewählt werden

Ich hoffe, diese Hilfe

$('input').on('blur', function() { 
 
    // check if the select is already present in the DOM 
 
    if (!$(".form-control").length) { 
 
    // get the input value 
 
    var inputVal = $(this).val(); 
 
    
 
    // we start the select form before the loop 
 
    var form = "<select class=\"form-control\"><option value=\"Option\">option</option>"; 
 

 
    // we loop from 2 to 8 
 
    for (i = 2; i < 9; i++) { 
 
     // if the selected value from the input match with the value of the loop var then we selected it 
 
     var selected = (i == inputVal ? " selected" : ""); 
 
     form += "<option value=\"" + i +"\"" + selected + ">" + i +" </option>"; 
 
    }; 
 

 
    // we the the closing tag for the select 
 
    form += "</select>"; 
 
    
 
    // we inject the form after the input 
 
    $(this).after(form); 
 
    } 
 
    
 
    $('input').on('change', function() { 
 
    // the value change in input so we get it 
 
    var newVal = $.trim($(this).val()); 
 
    // we remove all selected attribut of all option in select 
 
    $(".form-control option").removeAttr('selected'); 
 
    // then we add the selected attribut to the right option 
 
    $('.form-control option[value="'+ newVal +'"]').prop("selected","selected"); 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text">

+0

Anstatt nur Code schreiben, Es wird den Lesern helfen, wenn Sie erklären, warum Ihr Code das Problem löst. – 31piy

+0

Ich werde mehr meinen Code dann entschuldigen –

Verwandte Themen