简体   繁体   English

如何显示占位符文本而不是值文本

[英]How to make Placeholder text appear instead of Value text

I am trying to make a form that lets people input a URL in a text field that has a value and a different placeholder text. 我正在尝试创建一个表单,让人们在具有值和不同占位符文本的文本字段中输入URL。 I want the placeholder to show up instead of the value so if they don't type anything in the field it goes to the original field, but the links are very long so I don't want those to show up in the field. 我希望占位符显示而不是值,所以如果他们没有在字段中键入任何内容它会转到原始字段,但链接很长,所以我不希望它们出现在字段中。

I am using an input field with the value the url for a specific LinkedIn profile but I would like to have a placeholder to show LinkedIn's homepage url as it is much shorter. 我正在使用输入字段,其值为特定LinkedIn个人资料的url,但我希望有一个占位符来显示LinkedIn的主页网址,因为它更短。

<input 
  id="linkedIn" 
  type="text" 
  value="https://www.linkedin.com/company/stack-overflow" 
  placeholder="https://www.linkedin.com">

I am seeing only the value and not the placeholder text showing in the input field. 我只看到输入字段中显示的值而不是占位符文本。 I would like to see the placeholder instead of the value. 我想看看占位符而不是值。

You can use JS code to verify the value of input field. 您可以使用JS代码来验证输入字段的值。 Then if the textbox be empty it will get he original field. 然后如果文本框为空,它将获得原始字段。

<input 
id="linkedIn" 
type="text" 
placeholder="https://www.linkedin.com">
<input type="button" value="send" onclick="myFunction()">

<script>
    function myFunction(){
        var linkedIn = document.getElementById("linkedIn");
        if(linkedIn.value == ""){
            linkedIn.value = "https://www.linkedin.com/company/stack-overflow";
        }
    }
</script>

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

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