Javascript 按名称检索输入值,该名称位于id为的div中
我试图在用户键入文本字段时获取输入值,在div中使用Javascript 按名称检索输入值,该名称位于id为的div中,javascript,jquery,html,django,Javascript,Jquery,Html,Django,我试图在用户键入文本字段时获取输入值,在div中使用div的id=“search”和name=“vehicle”。我当前所做的操作将search\u text作为空字符串返回。我已将代码简化为帮助证明我的问题: $(文档).ready(函数(){ $(“#搜索”).keyup(函数(){ $.ajax({ 类型:“POST”, url:“http://localhost:8000/overview", 数据:{ “搜索文本”:$(“#搜索”).Children(“输入[name=vehicl
div
的id=“search”
和name=“vehicle”
。我当前所做的操作将search\u text
作为空字符串返回。我已将代码简化为帮助证明我的问题:
$(文档).ready(函数(){
$(“#搜索”).keyup(函数(){
$.ajax({
类型:“POST”,
url:“http://localhost:8000/overview",
数据:{
“搜索文本”:$(“#搜索”).Children(“输入[name=vehicle]”)
},
成功:搜索成功,
数据类型:“html”
});
});
});
函数searchSuccess(数据、文本状态、jqXHR){
$(“#搜索结果”).html(数据);
}
我尝试了以下方法:
'search_text': $('#search input')
及
谢谢首先,您在
$(“#搜索)
中缺少一个”
其次,如果希望从输入中获取值,则需要使用.val()
$(document).ready( function(){
$('#search').keyup(function() {
$.ajax({
type: "POST",
url: "http://localhost:8000/overview",
data: {
'search_text': $('#search input[name=vehicle]').val()
},
success: searchSuccess,
dataType: 'html'
});
});
});
演示
$(文档).ready(函数(){
$(“#搜索”).keyup(函数(){
console.log($('#搜索输入[name=vehicle]').val()
});
});代码>
或者您可以像这样使用oninput事件
document.querySelector('input[name=vehicle]').oninput=function(){
alert(this.value)
};
OR
$('input[name=vehicle]').oninput(function(){
alert(this.value)
});
那么,当用户在输入中输入时,您想要输入的值吗?对吗?请注意您在$(“#搜索)
子项中缺少一个”
,需要是子项,JS区分大小写。谢谢您的快速回答。我已经替换了代码行,但它仍然返回空字符串。@LewisHepburn尝试查看我添加的演示,您应该可以工作,尝试在Ajax之前添加console.log($(“#搜索输入[name=vehicle]”)。val())
,非常感谢Carsten!原来问题在于我的应用程序没有更新.js文件。
document.querySelector('input[name=vehicle]').oninput=function(){
alert(this.value)
};
OR
$('input[name=vehicle]').oninput(function(){
alert(this.value)
});