我在HTML中输入后有一个按钮,我想使用该按钮从该输入中检索值并执行操作。我面临的问题是我的jQuery没有找到这个价值。
HTML:
<div>
<input class="an-input" type="text"></input>
<button class="ui-state-default inputButton an-button">GO</button>
</div>
JS:
$('.an-button').click(function() {
var inputValue = $('.an-button').prev('.an-input').find('input').val();
window.open('http://a810-bisweb.nyc.gov/bisweb/JobsQueryByNumberServlet?passjobnumber='+inputValue+'&passdocnumber=&go10=+GO+&requestid=0');
});
答案 0 :(得分:2)
答案 1 :(得分:2)
尝试删除.find('input')
,因为.prev(".an-input")
应该返回input
元素。另请注意,<input />
标记是自动关闭的
$(".an-button").click(function() {
var inputValue = $(this).prev(".an-input").val();
console.log(inputValue)
//window.open('http://a810-bisweb.nyc.gov/bisweb/JobsQueryByNumberServlet?passjobnumber='+inputValue+'&passdocnumber=&go10=+GO+&requestid=0');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>
<input class="an-input" type="text" />
<button class="ui-state-default inputButton an-button">GO</button>
</div>
答案 2 :(得分:0)
$('button.an-button').click(function() {
var inputValue = $('.an-input:text').val();
... rest of code
})
答案 3 :(得分:0)
要定位兄弟姐妹,它必须是类似的类型。
请改为尝试:
$('.an-button').click(function() {
var inputValue = $('.an-button').parent().find('.an-input').val();
window.open('http://a810-bisweb.nyc.gov/bisweb/JobsQueryByNumberServlet?passjobnumber='+inputValue+'&passdocnumber=&go10=+GO+&requestid=0');
});
答案 4 :(得分:0)