Javascript 如何选择输入onClick的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10413704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:12:05  来源:igfitidea点击:

How to select value of input onClick?

javascript

提问by Szymon Toda

I have a <input type="text">and if user clicks inside it I want to make the content (value) of that box selected. How would I do that?

我有一个<input type="text">,如果用户在里面单击,我想选择该框的内容(值)。我该怎么做?

回答by Ehsan Khodarahmi

<input type="text" onclick="select()"/>

回答by VisioN

Try selectmethod:

尝试select方法:

document.getElementById("myinput").onclick = function() {
    this.select();
};

回答by Amit

You can try following code inside a javaScript method, and call the method onClick event of the textbox...

您可以尝试在 javaScript 方法中执行以下代码,并调用文本框的 onClick 事件方法...

function calledOnClick(){

    document.getElementById('test').select();
}

回答by Kunal Waghmare

You can try following jQuerycode which automatically call on page load

您可以尝试以下jQuery代码,它会在页面加载时自动调用

<script>
$(document).ready(function(){
    $('input').each(function(){
        $(this).click(function() {
            this.select();
        });
    });
    $("#return_date").focus();
});
</script>