【问题标题】:Is it possible to open a modal on input focus and enter a value to the input from the modal?是否可以在输入焦点上打开模态并从模态输入输入值?
【发布时间】:2013-01-08 09:43:09
【问题描述】:
jquery 新手用户在这里。我希望在输入被聚焦/选择时打开一个模式,并从模式中的按钮向输入输入一个值。
事情就是这样。
1. 集中输入。
2. 打开一个模态。
3. 通过使用按钮将值输入到输入中。
4. 关闭按钮时关闭模态框。
非常感谢。
【问题讨论】:
标签:
jquery
input
focus
modal-dialog
【解决方案1】:
你会想要prompt():
$('body').on('focus', 'input[name=myNameHere]', function(){
var input = prompt('The Question', 'Suggested answer');
$(this).val(input);
});
或者通过jQuery UI进行更高级的对话。
【解决方案2】:
这可能是您正在寻找的:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(function() {
$( "#dialog" ).dialog({modal: true, autoOpen:false});
$("#result").focus(function () {
$( "#dialog" ).dialog("open");
});
$('#accept').click(function (){
$('#result').val($('#source').val());
$('#dialog').dialog("close");
});
});
</script>
</head>
<body>
<input type="text" id="result">
</div>
<div id="dialog" title="Basic dialog">
Enter value <input type="text" id="source" /><br/>
<input type="button" id="accept" value="OK" />
</div>
</body>
</html>