简体   繁体   English

如何从输入中检索文本

[英]How to retrieve text from an input

I have an edit button, on click every td in the row will become input text, in my code it works, but the problem is that I can't find a way to retrieve the text in the input so i can save it later. 我有一个编辑按钮,单击该行中的每个td都将成为输入文本,在我的代码中它可以工作,但是问题是我找不到在输入中检索文本的方法,所以以后可以保存它。

   function Edit(clickedButton){
        var getTR = clickedButton.closest('tr');
        var getLength = getTR.childElementCount;
        var getTds = getTR.querySelectorAll("td")

        for (i in getTds) {
            if(i < (getLength-1)) {
                getTds[i].innerHTML = "<input  type='text' value='"+getTds[i].innerHTML+"'>";
            }       
        }

    }

Get it by the id 通过ID获取

document.getElementById('textbox_id').value

or 要么

Get it by the class 全班学习

document.getElementsByClassName('class_name')[element_index].value

or get it by the tag name 或通过标签名称获取

document.getElementsByTagName('tag_name')[whole_number].value

In your loop body add an event listener to the created input, for example bind to blur event to do something with the value once the input loses focus: 在循环主体中,将事件侦听器添加到创建的输入中,例如,一旦输入失去焦点,则绑定到blur事件以对值进行处理:

getTds[i].innerHTML = "<input  type='text' value='"+getTds[i].innerHTML+"'>";
getTds[i].getElementsByTagName('input')[0].addEventListener('blur', function(){
    console.log( this.value );
})

simplified example: http://jsfiddle.net/Lr08emod/ 简化的示例: http : //jsfiddle.net/Lr08emod/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM