javascript 如果单击按钮,则使用 Jquery 隐藏/取消隐藏文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6519212/
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
Using Jquery to hide/unhide the textbox if button is clicked
提问by Mr A
I have input button , what I want is if a user clicks on the button then textbox should appear.
我有输入按钮,我想要的是如果用户点击按钮然后文本框应该出现。
Below is the code which is not working :
以下是不起作用的代码:
<input type="submit" value="Add Second Driver" id="driver" />
<input type="text" id="text" />
$("#driver").click(function() {
$('#text').show();
}
});
Also the textbox should not be visible initially
此外,文本框最初不应该可见
回答by Talha Ahmed Khan
You can use toggle instead;
您可以改用切换;
$('#text').toggle();
With no parameters, the .toggle()
method simply toggles the visibility of elements
没有参数,该.toggle()
方法只是切换元素的可见性
回答by adamwtiko
回答by deepi
try this:
试试这个:
$(document).ready(function(){
$("#driver").click(function(){
$("#text").slideToggle("slow");
});
});
回答by Pronay Sharma
Make the textbox hidden when the page loads initially like this
当页面最初像这样加载时隐藏文本框
Code:
代码:
$(document).ready(function () {
$('#text').hidden();
});
Then your should work the way you want.
那么你应该按照你想要的方式工作。
回答by Nathan MacInnes
Try this:
试试这个:
<script type="text/javascript">
jQuery(function ($) {
$('#driver').click(function (event) {
event.preventDefault(); // prevent the form from submitting
$('#text').show();
});
});
</script>
<input type="submit" value="Add Second Driver" id="driver" />
<input type="text" id="text" />
回答by Gjorgji Tashkovski
<input type="submit" value="Add Second Driver" id="driver" />
<input type="text" id="text" style="display:none;" />
$("#driver").click(function() {
$('#text').css('display', 'block');
});
回答by Alex
$(function()
{
// Initially hide the text box
$("#text").hide();
$("#driver").click(function()
{
$("#text").toggle();
return false; // We don't want to submit anything here!
});
});