简体   繁体   English

如果输入了值,则获取输入值

[英]Get input value if values is pregiven

This gives me back the original value of the input not the edited one. 这给了我输入的原始值,而不是编辑的值。

<input type='text' value='test'>
this.parentNode.getElementsByTagName('input')[0].value

The js event in on onchage onchage中的js事件

I have it like this right now and I am not getting the input of it... 我现在有这样的东西,我没有得到它的输入...

var inputs=document.getElementById('wrap').getElementsByTagName('input');
        for (var i = inputs.length - 1; i >= 0; i--) {
            inputs[i].onchange=function(){
                var x=this.parentNode.appendChild(document.createElement("a"));
                x.innerHTML="upravit";
                x.onclick=function(){
                    var hr = new XMLHttpRequest();
                    console.log(this.parentNode.getElementsByTagName('input')[0].value);    

Even the console.log itself gives me back the default value instead of the new one... 甚至console.log本身也给了我默认值,而不是新值。

                        var vars = "menu=1&name=";
                        hr.open("POST", "Ajax/UpdateCategory.php", true);
                        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        hr.onreadystatechange = function(){
                            if(hr.readyState==4&&hr.status==200){
                                var return_data = hr.responseText;
                                if(return_data!="ok"){
                                    alert(return_data);
                                }
                            }
                        }
            hr.send(vars);
                    }
                }
            };

you can do like this 你可以这样

html html

<input type='text' value='test' onchange="dostuff(this)" />

js js

function dostuff(s) {

    alert(s.value);

}

here is a fiddle 这是一个小提琴

If this doesn't suit your case, please tell us how you are binding the js code in your question with the input in the html code. 如果这不适合您的情况,请告诉我们您如何将问题中的js代码与html代码中的输入绑定在一起。

Please try this demo to learn onchange handler: http://www.w3school.com.cn/tiy/t.asp?f=hdom_onchange 请尝试该演示以学习onchange处理程序: http : //www.w3school.com.cn/tiy/t.asp? f=hdom_onchange

and compare with this demo: http://www.w3school.com.cn/tiy/t.asp?f=hdom_onkeyup 并与该演示进行比较: http : //www.w3school.com.cn/tiy/t.asp? f= hdom_onkeyup

Then you will know that, just use onkeyup() instead will work. 然后您将知道,只需使用onkeyup()即可。

=== edited content === html: ===编辑的内容=== html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        window.onload = function () {
            var inputs=document.getElementById('wrap').getElementsByTagName('input');
            var x=document.createElement("a");
            var data;
            x.innerHTML="upravit";
            x.onclick=function(){
                alert(data);
            }
            for (var i = inputs.length - 1; i >= 0; i--) {
                inputs[i].onchange=function(){
                    this.parentNode.appendChild(x);
                    data = this.value;
                }
            }
        };
    </script>
</head>
<body>
<div id="wrap">
    <input type='text'>
    <input type='text'>
    <input type='text'>
</div>
</body>
</html>

Please run this html in your local environment, I think this meet your needs. 请在您的本地环境中运行此html,我认为这符合您的需求。

<input type='text' value='test' onchange="dostuff(this.value)" />

function dostuff(value) {    
    alert(value);    
}

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

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