admin管理员组文章数量:1325767
I'm trying to hide and show the password of the user but I'm having a hard time. Here is my markup:
<td>Username: <td>
<td><input type="text" class="form-control" value="<?php echo $row['Username'];?>"readonly><td>
<td><input type="button" class="btn btn-primary show" value="Show Password">
<input type="button" class="btn btn-primary hide" value="Hide Password" style="display:none;"><td>
</tr>
<tr class="password" style="display:none;">
<td>Password: <td>
<td><input type="text" class="form-control" value="<?php echo $row['Password']; ?>" readonly><td>
</tr>
And my jQuery
$(".show").click(function(){
$('.hide').show();
$('.show').hide();});
I'm trying to hide and show the password of the user but I'm having a hard time. Here is my markup:
<td>Username: <td>
<td><input type="text" class="form-control" value="<?php echo $row['Username'];?>"readonly><td>
<td><input type="button" class="btn btn-primary show" value="Show Password">
<input type="button" class="btn btn-primary hide" value="Hide Password" style="display:none;"><td>
</tr>
<tr class="password" style="display:none;">
<td>Password: <td>
<td><input type="text" class="form-control" value="<?php echo $row['Password']; ?>" readonly><td>
</tr>
And my jQuery
$(".show").click(function(){
$('.hide').show();
$('.show').hide();});
Share
Improve this question
edited Dec 6, 2014 at 3:21
rapidoodle
asked Jan 14, 2014 at 3:05
rapidoodlerapidoodle
3621 gold badge3 silver badges23 bronze badges
3 Answers
Reset to default 2Your HTML is invalid, you need to wrap a table tag around a tr tag or the tr and everything in it will return undefined in Javascript:
<table>
<tr>
<td>Username:
<td>
<td>
<input type="text" class="form-control" value="<?php echo $row['Username'];?>" readonly>
<td>
<td>
<input type="button" class="btn btn-primary show" value="Show Password">
<input type="button" class="btn btn-primary hide" value="Hide Password" style="display:none;">
<td>
</tr>
<tr class="password" style="display:none;">
<td>Password:
<td>
<td>
<input type="text" class="form-control" value="<?php echo $row['Password'];?>" readonly>
<td>
</tr>
</table>
and to plete your JQuery, you could use this:
$(".show").click(function () {
$('.hide').show();
$(this).hide();
$('.password').eq(0).show();
});
$('.hide').click(function () {
$('.show').show();
$(this).hide();
$('.password').eq(0).hide();
});
DEMO
You need a [unique] identifier for your password textbox and for your jQuery selector to point to it.
Example:
<td><input type="text" class="form-control" id="pwd" value="<?php echo $row['Password']; ?>" readonly><td>
And your jQuery selector should therefore look for am element with id="pwd"
:
$(".show").click(function(){
$('#pwd').show();
});
$(".hide").click(function(){
$('#pwd').hide();
});
You also can use .toggle() method like
$(".show").click(function () {
$('.hide').toggle();
$(this).toggle();
$('.password').eq(0).toggle();
});
$('.hide').click(function () {
$('.show').toggle();
$(this).toggle();
$('.password').eq(0).toggle();
});
Working fiddle
Also do some little changes to HTML as suggested by @Man of Snow..
本文标签: javascriptShow textbox when button is clickStack Overflow
版权声明:本文标题:javascript - Show textbox when button is click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742189767a2429993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论