我有一个文本框,如下所示,
<input type="text" name="company" id="company" required />
我的问题是,每当我键入或选择值时,我都希望使用jquery获取文本框中的值。
我尝试了很多但没有效果。我试过的jquery在下面,
$(document).ready(function(){
var input = $('#company').val();
$("#tag").autocomplete("autocomplete1.php?str="+input, {
selectFirst: true
});
});
以上代码无效。有什么方法吗?请帮助。 提前谢谢。
答案 0 :(得分:2)
您需要在更改上绑定Jquery事件,例如
$(document).ready(function(){
$('#company').focus(function(){
var input = $('#company').val();
$("#tag").autocomplete("autocomplete1.php?str="+input, {
selectFirst: true
});
});
});
您可以绑定许多其他事件,here是Jquery的官方文档。
答案 1 :(得分:1)
尝试类似:
$(document).ready(function() {
$('#company').on('change : focus : keypress', function() {
// this.value is what you need
$('#result').html(this.value);
});
});
答案 2 :(得分:0)
您也可以使用on
blur
将其focusout
作为替代方式,如下所示:
$('#company').on('blur',function(){
var input = $(this).val();
//Other codes
});