最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Show hidden input javascriptjquery - Stack Overflow

programmeradmin2浏览0评论

Why is the hidden form not shown when it looses focus? The alert is ing up nicely when leaving the input but the other hidden form is still not there.

html

<body>
   <input type="text" id="myinput" value="">
   <input type="hidden" id="validation_message_email" value="enter a valid email">
</body>

javascript

window.onload = function() {
   $("#myinput").blur(myAlert);
};

function myAlert() {
   alert("This input field has lost its focus.");
   $("#validation_message_email").show();
}

Why is the hidden form not shown when it looses focus? The alert is ing up nicely when leaving the input but the other hidden form is still not there.

html

<body>
   <input type="text" id="myinput" value="">
   <input type="hidden" id="validation_message_email" value="enter a valid email">
</body>

javascript

window.onload = function() {
   $("#myinput").blur(myAlert);
};

function myAlert() {
   alert("This input field has lost its focus.");
   $("#validation_message_email").show();
}
Share Improve this question edited Jun 1, 2016 at 13:25 Mohammad 21.5k16 gold badges57 silver badges85 bronze badges asked Jun 1, 2016 at 12:43 javajava 1,1652 gold badges29 silver badges52 bronze badges 3
  • Its hidden element notice type="hidden" I think you need <span style="display:none" id="validation_message_email">enter a valid emai</span> – Satpal Commented Jun 1, 2016 at 12:44
  • why have you taken "validation_message_email" hidden? – Jayesh Chitroda Commented Jun 1, 2016 at 12:45
  • type='hidden' doesn't directly implies that the element is hidden, it means the control itself is a type known as 'hidden'. You cannot change it, so its remended you use a <div> or <span> tag, set the css as display:none initially, and then later call the jquery show() function on the element. – Ahs N Commented Jun 1, 2016 at 12:47
Add a ment  | 

6 Answers 6

Reset to default 5

You can't display a hidden input like that.A span will suit better for this purpose,

<input type="text" id="myinput" value="">
<span style="display:none"  id="validation_message_email">enter a valid email</span>

validation_message_email doesn't have its display style property as none, so show() will not make it visible from type="hidden".

You need to replace

$("#validation_message_email").show();

with

$("#validation_message_email").attr( "type", "text" );

However, if the intent is to only show a message, then you don't need to use a hidden input for the same.

<body>
   <input type="text" id="myinput" value="">
</body>

and

window.onload = function() {
   $("#myinput").blur(function(){
      alert("This input field has lost its focus.");
      $(this).append('<span id="emailValidationMessage">enter a valid email</span>')
   });
   $("#myinput").focus(function(){
      $("#emailValidationMessage").remove();
   });
};

No need to use type="hidden" as hidden elements are not display:none they are hidden by default.

Use type="text" and hide it with css and show where you want

<input type="text" id="myinput" value="" style="display:none;">

use like this

<input type="text" id="myinput" value="">
<input type="hidden" id="validation_message_email" value="enter a valid email">

<script>   
window.onload = function() {
    $("#myinput").blur(myAlert);
};

function myAlert() {
   $("#validation_message_email").attr("type","text");
}
</script>
 <div class="form-group" id="usernamediv">

 <input class="form-control" name="username" id="username"
     placeholder="Username" type="text"  required=""> </div>
 <div class="form-group" id="passworddiv">
  <input name="password" class="form-control" id="password" placeholder="Password"  type="password" required="">
  </div>
<button id="#loginButton">Login</button>
<button id="#forgotpassword">Forgot Password</button>


<script>
$("#forgotpassword").hide();
$("#forgotpassword").click(function(e){
$("#loginButton").hide();
$("#usernamediv").show();
$("#passworddiv").hide();
})


</script>

Check this jsfiddle link, it might help you.

$("#myinput").blur( function(){

myAlert();

});

function myAlert() {
   $("#validation_message_email").attr("type", "text");
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<input type="text" id="myinput" value="">
<input type="hidden" id="validation_message_email" value="enter a valid email">

发布评论

评论列表(0)

  1. 暂无评论