抱歉,成为这样的初学者,但我花了几天时间尝试寻找答案,现在我认为您需要专业知识。
我尝试做类似“如果单击li,则用li编写的文本将显示在搜索框中(ID'#input#),并自动启动请求。”
我的脚本运行良好,但请求/搜索未启动。在此过程的最后一步,我阻止了该脚本。
非常感谢您的帮助。
的HTML
<ul id="test">
<li>Hello world</li>
<li>Hello world 2</li>
</ul>
Javascript,JQuery
<script type="text/javascript">
$(document).ready(function(){
$("#test li").click(function (){
varindex = $(this).text();
$('#input').val("Text is in the search box!"+" "+varindex);
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#test li").click(function (){
$('#input').focus();
$('#input').trigger(jQuery.Event('key', {which: 13}));
});
});
</script>
最佳答案
这有效:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script>
$(document).ready( function () {
$('#test li').click ( function () {
$('#input').attr("value", $(this).text ());
$('#myform').submit ();
});
});
</script>
</head>
<body>
<ul id="test">
<li> Hello world </li>
<li> Hello world 2 </li>
</ul>
<form id="myform" action="xxx.yyy.zz" method="get">
<input id="input" name="myinput" type="text" />
</form>
</body>
关于javascript - jQuery Focus()和自动Keypress(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24838782/