简体   繁体   中英

jQuery Focus() and automatic Keypress()

Sorry for being such a beginner but I spent days try to find an answer and now I think your expertise is needed.

I try to do something like "If I click on li, then the text written in li is displayed in a search box (ID '#input#) and automatically the request is launched."

My script works fine but the request/search IS NOT launched.I blocked at the last step of the process.

Thank you very much for your help.

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>

This works:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM