2016-08-28 3 views
1

Ich habe 4 Radio-Eingang.jQuery Hotkeys funktioniert nur einmal

Und ich habe ihnen Hotkeys zugewiesen. (Hotkeys sind 1,2,3 und 4)

Hier ist mein html Code:

<label> 
<input type="radio" value="2" id="1" name="1">[c2] 
</label> 
<label> 
<input type="radio" value="4" id="2" name="1">[c4] 
</label> 
<label> 
<input type="radio" value="1" id="3" name="1">[c1] 
</label> 
<label> 
<input type="radio" value="3" id="4" name="1">[c3] 
</label> 

und Javascript Code:

$(document).keypress(function(e) { 

    if(e.charCode == 49) { 
    console.log("1"); 
    $('input:radio[id=1]').attr('checked',true); 
    } 

    if(e.charCode == 50) { 
    console.log("2"); 
    $('input:radio[id=2]').attr('checked',true); 
    } 

    if(e.charCode == 51) { 
    console.log("3"); 
    $('input:radio[id=3]').attr('checked',true); 
    } 

    if(e.charCode == 52) { 
    console.log("4"); 
    $('input:radio[id=4]').attr('checked',true); 
    }     
}); 

Sie auch sehen können, ich Setzen Sie console.log Ereignisse in Skript, nur um zu beobachten.

Das Seltsame für mich, ich kann Redio-Tasten mit ihren Hotkeys nur einmal pro Optionsfeld auswählen.

*

Wenn ich 1 drücken, wählt es die richtige Checkbox und gibt mir das richtige Konsolenprotokoll.

Dann drücke ich 2, und funktioniert das gleiche.

Aber wenn ich wieder 1 drücke, gibt es mir das Konsolenprotokoll, aber Hotkey funktioniert nicht.

Hier ist jsfiddle: https://jsfiddle.net/f07evno0/

Wie kann ich Hotkeys machen mehr Arbeit als einmal in einer Sitzung?

Antwort

4

Sie können

$("input:radio[id=2]").prop("checked", true) 

$(document).keypress(function (e) { 
 

 
     if (e.charCode == 49) { 
 
      console.log("1"); 
 
      $('input:radio[id=1]').prop('checked', true); 
 
     } 
 

 
     if (e.charCode == 50) { 
 
      console.log("2"); 
 
      $('input:radio[id=2]').prop('checked', true); 
 
     } 
 

 
     if (e.charCode == 51) { 
 
      console.log("3"); 
 
      $('input:radio[id=3]').prop('checked', true); 
 
     } 
 

 
     if (e.charCode == 52) { 
 
      console.log("4"); 
 
      $('input:radio[id=4]').prop('checked', true); 
 
     } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<label> 
 
    <input type="radio" value="2" id="1" name="1">[c2] 
 
</label> 
 
<label> 
 
    <input type="radio" value="4" id="2" name="1">[c4] 
 
</label> 
 
<label> 
 
    <input type="radio" value="1" id="3" name="1">[c1] 
 
</label> 
 
<label> 
 
    <input type="radio" value="3" id="4" name="1">[c3] 
 
</label>

verwenden Ausprobieren https://jsfiddle.net/f07evno0/1/

+1

Schauen Sie sich das zusätzlich an: http://stackoverflow.com/questions/5874652/prop-vs-attr Es gibt die Erklärung. –

+0

Sie sollten diese Antwort erweitern und erklären, warum das im Vergleich zum Ansatz von OP funktioniert :) –

1

ich nicht den Grund herauszufinden, aber dieser Code gearbeitet:

$(document).keypress(function(e) { 

    if(e.charCode == 49) { 
    console.log("1"); 
    //$('input:radio[id=1]').attr('checked',true); 
    $('input:radio[id=1]')[0].checked= "checked" 
    } 

    if(e.charCode == 50) { 
    console.log("2"); 
    //$('input:radio[id=2]').attr('checked',true); 
    $('input:radio[id=2]')[0].checked= "checked" 
    } 

    if(e.charCode == 51) { 
    console.log("3"); 
    //$('input:radio[id=3]').attr('checked',true); 
    $('input:radio[id=3]')[0].checked= "checked" 
    } 

    if(e.charCode == 52) { 
    console.log("4"); 
    //$('input:radio[id=4]').attr('checked',true); 
    $('input:radio[id=4]')[0].checked= "checked" 
    }     
});