2016-08-16 3 views
0

Ich habe eine Schaltfläche, die Nummer "1" angezeigt, wenn geklickt und drei Textfelder. Ich möchte, dass beim Klicken auf die Schaltfläche die Nummer in dem Textfeld mit dem Fokus angezeigt wird. Kann mir bitte jemand helfen?Tippen Sie auf Textfeld, wenn es Fokus mit Schaltfläche Javascript

function run(){ 
 
    
 
document.calc.txt1.value += "1"; 
 
}
<input type=button name="btn1" value="1" OnClick="run()"id="button"><br /> 
 
<form name="calc"> 
 
<input type="text" id="txt1" name="txt1"> 
 
<input type="text" id="txt2" name="txt2"> 
 
<input type="text" id="txt3" name="txt3"> 
 
</form>

T3" >

+0

No '' Element wird Fokus haben, wenn die'

Antwort

0

Wenn Sie auf eine Schaltfläche klicken, werden die vorherigen input Losen konzentrieren Sie könnten versuchen, die letzte fokussierte Eingabeelement vor dem Klick zu speichern.

(Dies benötigt etwas mehr Arbeit)

var lastFocus = null; 
 

 
document.addEventListener("blur", function(event) { 
 
    // Here, you'll need to find out if the blurred element 
 
    // was one of your valid inputs. It's probably best to 
 
    // identify them by a class name 
 
    if (event.target.type === "text") { 
 
    lastFocus = event.target; 
 
    } 
 
}, true); 
 

 
function run() { 
 
    // When the user hasn't yet focused on a text input, 
 
    // the first one is used by default 
 
    (lastFocus || document.calc.txt1).value += "1"; 
 
}
<input type=button name="btn1" value="1" OnClick="run()"id="button"><br /> 
 
<form name="calc"> 
 
<input type="text" id="txt1" name="txt1"> 
 
<input type="text" id="txt2" name="txt2"> 
 
<input type="text" id="txt3" name="txt3"> 
 
</form>

0

var currId; 
 
function setId(curr){ 
 
    currId=curr.id; 
 
    } 
 
function run(){ 
 
    if(currId) 
 
    { 
 
     document.getElementById(currId).value +='1'; 
 
     } 
 
//document.calc.txt1.value += "1"; 
 
    //document.activeElement.value += "1"; 
 
}
<input type=button name="btn1" value="1" OnClick="run()"id="button"><br /> 
 
<form name="calc"> 
 
<input type="text" id="txt1" name="txt1" onblur="setId(this)"> 
 
<input type="text" id="txt2" name="txt2" onblur="setId(this)"> 
 
<input type="text" id="txt3" name="txt3" onblur="setId(this)"> 
 
</form>

0

Ok, so sollte dieser Code-Schnipsel tun, was Sie wollen. Beachten Sie jedoch, dass das Eingabefeld immer unscharf wird, wenn Sie auf die Schaltfläche klicken.

Im Wesentlichen, was dieser Code hier tut, ist das onfocus-Attribut gesetzt, damit Sie herausfinden können, welches Eingabefeld zuletzt fokussiert wurde und nicht auf welches Eingabefeld IS fokussiert ist, denn keines ist. Außerdem würde ich empfehlen, den Button zu einem "Button" -Tag zu ändern, weil er ihn in Bezug auf den Tag-Namen von den anderen Eingabefeldern trennt.

Ich hoffe, dies hat geholfen, und lassen Sie mich wissen, wenn Sie irgendwelche Fragen haben.

var inputs = document.getElementsByTagName('input'); 
 
    for(var i = 1 ; i < inputs.length; i ++){ 
 
     inputs[i].onfocus = function(){ 
 
       this.setAttribute('class','focused'); 
 
     } 
 
    } 
 
    function run(){ 
 
    var inputBox = document.getElementsByClassName('focused')[0]; 
 
    if(inputBox){ 
 
     inputBox.value += "1"; 
 
     inputBox.setAttribute('class','blurred'); 
 
    } 
 
    } 
 
    
 

 
<input type=button name="btn1" value="1" OnClick="run()"id="button"><br  /> 
 
    <form name="calc"> 
 
    <input type="text" id="txt1" name="txt1"> 
 
    <input type="text" id="txt2" name="txt2"> 
 
    <input type="text" id="txt3" name="txt3"> 
 
    </form> 
 
    
 

Verwandte Themen