简体   繁体   English

我有2个文本框和一个按钮! 当我单击按钮时,它应该在所选文本框中写1,但是我的代码不起作用

[英]I have 2 text Boxes and one button! When I click on button it should write 1 in the chosen text box but my code is not working

I have 2 text Boxes and one button! 我有2个文本框和一个按钮! When I click on button it should write 1 in the chosen text box but my code is not working. 当我单击按钮时,它应该在所选文本框中写1,但是我的代码不起作用。

my code: 我的代码:

function one() {
var number = "1";


if (document.getElementById("txt1").focused) {
    document.getElementById("txt1").value = number;
}
else if (document.getElementById("txt2").focused) {
    document.getElementById("txt2").value = number;
}
else {

}

You can do some thing like this: 您可以执行以下操作:

<input type="text" onfocus="onFocus(this)" id="itext1"></input>
<input type="text" onfocus="onFocus(this)" id="itext2"></input>
<button onclick="setValue()">ClickMe</button>
<script>
    var selectedDOM = undefined;

    function setValue() {
        if (selectedDOM) { //selecteddom is present set text to it
            selectedDOM.value = 1;
        }
    }

    function onFocus(e) {
        selectedDOM = e;//register the last dom selected inside the variable.
    }
</script>

Full working code here 完整的工作代码在这里

You can use onfocus property to perform this instead. 您可以使用onfocus属性来执行此操作。

HTML 的HTML

<input type="text" onfocus="myFocusFunction(this)">
<input type="text" onfocus="myPlaceholderFunction(this)">

Javascript Java脚本

function myFocusFunction(x) {
   x.value = "1";    
}

function myPlaceholderFunction(x) {
   x.placeholder = "1";    
}

Above are two approaches to set value in text box when focused. 上面是聚焦时在文本框中设置值的两种方法。 You can apply any of them prior will add a value and later will add a placeholder instead. 您可以先应用任何一个,然后再添加一个值,然后再添加一个占位符。

Working Demo : JSFiddle 工作演示JSFiddle

You can do like this 你可以这样

<div>
    <input type="text" id="txt1">
    <input type="text" id="txt2">    
        <button type ="button" id='button'>Click Me </button>
</div>

JavaScript 的JavaScript

(function(){
    var number ='1';
    var _thisId=null;   
     var _button = document.getElementById('button');
     _button.addEventListener('mouseover',function(){
     _thisId = document.activeElement.id;
    },false)

    _button.addEventListener('click',function(){
        if(_thisId =='txt1'){
                document.getElementById("txt2").value = "";
                document.getElementById("txt1").value = number;     
            }
            else if(_thisId =='txt2'){
                document.getElementById("txt1").value = "";
                document.getElementById("txt2").value = number;     
                }
        })
}())

jsfiddle jsfiddle

我有<div>具有相同 class 和按钮的盒子。 单击按钮时,应将文本框添加到相应的框中</div><div id="text_translate"><p><a href="https://i.stack.imgur.com/tP1dL.png" rel="nofollow noreferrer">在按下第二个“添加任务”按钮时,应将一个文本框添加到第二个 div,类似于 3rd、4th</a>我如何引用特定的 div 元素? 无论按下哪个按钮,我在下面所做的都会将文本框添加到第一个 div。</p><p> HTML</p><pre> &lt;body&gt; &lt;div id="project"&gt; LIST IT &lt;/div&gt; &lt;div id="board"&gt; Java Project &lt;/div&gt; &lt;template id="listTemp"&gt; &lt;form class="frm" &gt; &lt;input type="text" placeholder="Enter list name"&gt; &lt;/form&gt; &lt;button type="button" name="task" class="lst btn btn-secondary" onclick="addTask()"&gt;Add Task&lt;/button&gt; &lt;/template&gt; &lt;button type="button" name="list" class="btn btn-secondary" onclick="addList()"&gt;Add List&lt;/button&gt; &lt;/body&gt; &lt;script src="index.js" charset="utf-8"&gt;&lt;/script&gt;</pre><p> JAVASCRIPT</p><pre> function addTask(){ var input = document.createElement('input'); input.placeholder="Enter task here"; input.type="text"; var br = document.createElement('br'); var parent = document.getElementsByClassName('frm')[0]; // parent.insertBefore(input,document.getElementsByClassName('lst')[0]); parent.appendChild(input); } function addList(){ var div = document.createElement('div'); div.innerHTML = document.getElementById('listTemp').innerHTML; div.className = 'ListBox'; document.getElementsByTagName('body')[0].appendChild(div); }</pre></div> - I have <div> boxes with same class with buttons. On button click a textbox should be added to the respective box

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击打开按钮时,页面中应出现一个文本框 - when i click the open button a text box should appear in the page 我有<div>具有相同 class 和按钮的盒子。 单击按钮时,应将文本框添加到相应的框中</div><div id="text_translate"><p><a href="https://i.stack.imgur.com/tP1dL.png" rel="nofollow noreferrer">在按下第二个“添加任务”按钮时,应将一个文本框添加到第二个 div,类似于 3rd、4th</a>我如何引用特定的 div 元素? 无论按下哪个按钮,我在下面所做的都会将文本框添加到第一个 div。</p><p> HTML</p><pre> &lt;body&gt; &lt;div id="project"&gt; LIST IT &lt;/div&gt; &lt;div id="board"&gt; Java Project &lt;/div&gt; &lt;template id="listTemp"&gt; &lt;form class="frm" &gt; &lt;input type="text" placeholder="Enter list name"&gt; &lt;/form&gt; &lt;button type="button" name="task" class="lst btn btn-secondary" onclick="addTask()"&gt;Add Task&lt;/button&gt; &lt;/template&gt; &lt;button type="button" name="list" class="btn btn-secondary" onclick="addList()"&gt;Add List&lt;/button&gt; &lt;/body&gt; &lt;script src="index.js" charset="utf-8"&gt;&lt;/script&gt;</pre><p> JAVASCRIPT</p><pre> function addTask(){ var input = document.createElement('input'); input.placeholder="Enter task here"; input.type="text"; var br = document.createElement('br'); var parent = document.getElementsByClassName('frm')[0]; // parent.insertBefore(input,document.getElementsByClassName('lst')[0]); parent.appendChild(input); } function addList(){ var div = document.createElement('div'); div.innerHTML = document.getElementById('listTemp').innerHTML; div.className = 'ListBox'; document.getElementsByTagName('body')[0].appendChild(div); }</pre></div> - I have <div> boxes with same class with buttons. On button click a textbox should be added to the respective box 我有两个复选框“接受”和“拒绝”并提交按钮。 当我单击提交按钮时,它应该检查勾选了哪个复选框 - I have two check boxes “Accept” and “Decline” and submit button. When i click on submit button it should check the which check box is ticked 在我的布局中,当我单击编辑文本时,在编辑文本下方有一个编辑文本和一个按钮,然后在软输入上方显示布局 - In my layout one edit text and one button below edit text when i click on edit text then layout show above the soft input 当按钮单击JS时显示2个文本框 - Showing 2 text boxes when button click JS 为什么我单击按钮时脚本不打印文本 - why is my script not printing text when i click the button 当我输入一个文本框时,会填满一些文本框 - I have a few text boxes that fill when I type in one 单击链接按钮时更改其文本 - change the text for link button when i click on it 单击按钮时更改按钮的文本 - change text of button when i click it 如何通过单击将按钮的值添加到文本框? - How can I add the value from button to the text box by click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM