如何在jQuery中打开android上的键盘?
我有这个样本: 代码HTML:如何在jQuery中打开android上的键盘?,jquery,html,css,Jquery,Html,Css,我有这个样本: 代码HTML: <button id="btn">OPEN INPUT</button> <input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text"> #search{ display:none; } jQuery("#btn").on('click', function(){ jQuery('#sear
<button id="btn">OPEN INPUT</button>
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
#search{
display:none;
}
jQuery("#btn").on('click', function(){
jQuery('#search').show();
jQuery('#search').trigger('click');
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
jQuery("button").on('click', function(){
jQuery('#search').focus();
});
代码jQuery:
<button id="btn">OPEN INPUT</button>
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
#search{
display:none;
}
jQuery("#btn").on('click', function(){
jQuery('#search').show();
jQuery('#search').trigger('click');
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
jQuery("button").on('click', function(){
jQuery('#search').focus();
});
请在手机上打开此示例
我想要的是当用户点击按钮时打开android键盘
这个例子应该适用于Iphone和Android
我试图模拟点击搜索输入,但似乎不起作用。。。你能帮我解决这个问题吗
更新:
<button id="btn">OPEN INPUT</button>
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
#search{
display:none;
}
jQuery("#btn").on('click', function(){
jQuery('#search').show();
jQuery('#search').trigger('click');
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
jQuery("button").on('click', function(){
jQuery('#search').focus();
});
我刚刚更改了初始示例以尝试一种新方法,但它似乎也不起作用
代码HTML:
<button id="btn">OPEN INPUT</button>
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
#search{
display:none;
}
jQuery("#btn").on('click', function(){
jQuery('#search').show();
jQuery('#search').trigger('click');
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input id="search" type="text" name="q" value="" placeholder="Demo search" class="input-text">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
jQuery("button").on('click', function(){
jQuery('#search').focus();
});
只要文本输入字段处于焦点位置,Android就会打开键盘。 试着这样做:
jQuery('#search').focus();
我试着按照你说的去做,我对示例做了一点修改,并告诉我为什么.focus()的函数不起作用;问题是调用.focus()时,模态尚未完成动画制作;下面是正确的代码:jQuery(“按钮”).on('click',function(){setTimeout(()=>{jQuery('search').focus()},300});好的,但是这个.focus()在iphone上不起作用,有什么可以替代它在iphone上起作用呢?你要求的是Android。。。尝试放置event.preventDefault();在您的点击处理程序中,如下所示:jQuery(“按钮”).on('click',function(event){event.preventDefault();setTimeout(()=>{jQuery('#search').focus()},300});我在上面说过,它应该可以在Iphone和Android上运行。代码我把它,但它似乎没有改变。