在文本框中输入文本 [英] inputing text in textbox

查看:136
本文介绍了在文本框中输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法很简单,只是假设将所选文本输入到文本框中。



HTML:

 < input type =textid =testemaxlength =50> 
< select>
< option onClick =nelson> nelson< / option>
< / select>

JavaScript:

 函数nelson(){
document.getElementById('teste')。value = +nelson;
}

预先致谢

解决方案

DEMO: jsFiddle

a>

HTML:
$ b

 < input type =textid =testemaxlength =50/> 
< select onchange =selectedItemChange(this)>
< option value =nelson> nelson< / option>
< option value =justin> justin< / option>
< / select>

JS:

 函数selectedItemChange(sel){
document.getElementById('teste')。value = sel.value;
}

解释:

 < option onClick =nelson> nelson< / option> 




  • 有三个原因:
    $ b


    1. onclick 优先于 onClick code>为了一致

    2. nelson 需要更改为 nelson()实际调用该函数。

    3. 因为我们正在处理选择 html元素,所以最好使用
      onchange




document.getElementById('teste')。value = +nelson;




  • 许多人指出适当的操作符是 + = =



  • $ b

    要设置初始值,请执行以下操作:

    DEMO: jsFiddle

     < input type =textid =testemaxlength = 50/> 
    < select id =select-peopleonchange =selectedItemChange(this)>
    < option value =nelson> nelson< / option>
    < option value =justin> justin< / option>
    < / select>

    JS

     函数selectedItemChange(sel){
    document.getElementById('teste')。value = sel.value;


    window.onload = function(){
    document.getElementById('teste')。value = document.getElementById(select-people)。value;
    }


    Can anyone please tell me why this jsFiddle doesn't work?

    The idea is simple is just suppose to input the selected text into the textbox..

    HTML:

    <input type="text" id="teste" maxlength="50">
    <select>
        <option onClick="nelson">nelson</option>
    </select>
    

    JavaScript:

    function nelson(){
        document.getElementById('teste').value =+ "nelson"; 
    }
    

    Thanks in advance

    解决方案

    DEMO: jsFiddle

    HTML:

    <input type="text" id="teste" maxlength="50" />
    <select onchange="selectedItemChange(this)">
        <option value="nelson">nelson</option>
        <option value="justin">justin</option>
    </select>
    

    JS:

    function selectedItemChange(sel) {
        document.getElementById('teste').value = sel.value;
    }
    

    Explanation:

    <option onClick="nelson">nelson</option>
    

    • was changed for three reasons:

      1. onclick is preferred to onClick for consistency
      2. nelson needed to be changed to nelson() to actually call the function.
      3. Since we are dealing with a select html element it is better to use the onchange event on the root.

    document.getElementById('teste').value =+ "nelson";

    • As many others have pointed out the proper operator is += or =

    To set initial value do the following

    DEMO: jsFiddle

    HTML

    <input type="text" id="teste" maxlength="50" />
    <select id="select-people" onchange="selectedItemChange(this)">
        <option value="nelson">nelson</option>
        <option value="justin">justin</option>
    </select>
    

    JS

    function selectedItemChange(sel) {
        document.getElementById('teste').value = sel.value;
    }
    
    window.onload=function(){
        document.getElementById('teste').value = document.getElementById("select-people").value;
    }
    

    这篇关于在文本框中输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆