简体   繁体   English

如何使用Jquery获取输入文本值

[英]How to get input text value using Jquery

I have the following code, in mydata.jsp value of q is empty. 我有以下代码,在mydata.jsp中q的值为空。 What is the correct way to pass value of inputId to mydata.jsp? 将inputId的值传递给mydata.jsp的正确方法是什么?

<input type="text" size="25" name="inputId" id="inputId" />

and Jquery script as 和Jquery脚本一样

 <script type="text/javascript">
$(function() {
    $("#inputId").autocomplete({

        source: "mydata.jsp?q="+$( "#inputId" ).val(),
        select:function(event,ui) {
            $("#hid").val(ui.item.email)
        }
    });
});


</script>

When you pass $( "#inputId" ).val() as an argument to .autocomplete() , you're getting the initial value from that field before anything has been typed. 当您将$( "#inputId" ).val()作为参数传递给.autocomplete() ,您将在输入任何内容之前从该字段获取初始值。 That would explain why it is empty. 这可以解释为什么它是空的。

If you use the string version of autocomplete, it can automatically add the typed value to the URL. 如果您使用自动填充的字符串版本,它可以自动将键入的值添加到URL。 See the doc for the string version of source here . 见字符串版本的文档source 在这里

If you want the URL for the source to be a dynamically generated URL, then you can also pass a function as the source and the code in that function can generate the URL/alternatives. 如果您希望source的URL是动态生成的URL,那么您还可以将函数作为source传递,并且该函数中的代码可以生成URL /替代。 See the doc for the source option here . 请参阅该文档source选择这里

 $('#inputId').keyup(function()
{
var str=$(this).val();
if(str!='')
{
  $.ajax({
      type:"GET",
      url:"mydata.jsp",
      data:"str="+str,
      success: function(data){
          alert(data);
     }

  });
}
});

Try like this. 试试这样吧。 hope it will give you some solution. 希望它会给你一些解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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