Text in HTML Field to disappear when clicked?

后端 未结 7 579
悲&欢浪女
悲&欢浪女 2020-12-13 18:12

I can easily create a html input field that has text already in it. But when the user clicks on the input field the text doesn\'t disappears but stays there. The user then h

相关标签:
7条回答
  • 2020-12-13 18:34

    Simple as this: <input type="text" name="email" value="e-mail..." onFocus="this.value=''">

    0 讨论(0)
  • 2020-12-13 18:45

    Use the placeholder attribute. The text disappears when the user starts typing. This is an example of a project I am working on:

    <div class="" id="search-form">
        <input type="text" name="" value="" class="form-control" placeholder="Search..." >
    </div>
    
    0 讨论(0)
  • 2020-12-13 18:47

    This is as simple I think the solution that should solve all your problems:

    <input name="myvalue" id="valueText" type="text" value="ENTER VALUE">
    

    This is your submit button:

    <input type="submit" id= "submitBtn" value="Submit">
    

    then put this small jQuery in a js file:

    //this will submit only if the value is not default
    $("#submitBtn").click(function () {
        if ($("#valueText").val() === "ENTER VALUE")
        {
            alert("please insert a valid value");
            return false;
        }
    });
    
    //this will put default value if the field is empty
    $("#valueText").blur(function () {
        if(this.value == ''){ 
            this.value = 'ENTER VALUE';
        }
    }); 
    
     //this will empty the field is the value is the default one
     $("#valueText").focus(function () {
        if (this.value == 'ENTER VALUE') {
            this.value = ''; 
        }
    });
    

    And it works also in older browsers. Plus it can easily be converted to normal javascript if you need.

    0 讨论(0)
  • 2020-12-13 18:48

    To accomplish that, you can use the two events onfocus and onblur:

    <input type="text" name="theName" value="DefaultValue"
      onblur="if(this.value==''){ this.value='DefaultValue'; this.style.color='#BBB';}"
      onfocus="if(this.value=='DefaultValue'){ this.value=''; this.style.color='#000';}"
      style="color:#BBB;" />
    
    0 讨论(0)
  • 2020-12-13 18:52

    What you want to do is use the HTML5 attribute placeholder which lets you set a default value for your input box:

    <input type="text" name="inputBox" placeholder="enter your text here">

    This should achieve what you're looking for. However, be careful because the placeholder attribute is not supported in Internet Explorer 9 and earlier versions.

    0 讨论(0)
  • 2020-12-13 18:53

    How about something like this?

    <input name="myvalue" type="text" onfocus="if(this.value=='enter value')this.value='';" onblur="if(this.value=='')this.value='enter value';">

    This will clear upon focusing the first time, but then won't clear on subsequent focuses after the user enters their value, when left blank it restores the given value.

    0 讨论(0)
提交回复
热议问题