Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时,如何从其正上方的文本框中获取文本_Javascript_Jquery_Html - Fatal编程技术网

Javascript 单击按钮时,如何从其正上方的文本框中获取文本

Javascript 单击按钮时,如何从其正上方的文本框中获取文本,javascript,jquery,html,Javascript,Jquery,Html,我有一些代码,比如: <input type="text" class="searchpersontxtbox" /> <input type="button" value="Find" class="findpersonbtn btn" disabled="disabled" /> 因此,当点击按钮时,我试图在搜索框中获取文本,并向屏幕发出警报 需要注意的一点是,页面上可能有多个带有“searchpersontxtbox”类的搜索框 所以我想从按钮正上方的文本框中获取

我有一些代码,比如:

<input type="text" class="searchpersontxtbox" />
<input type="button" value="Find" class="findpersonbtn btn" disabled="disabled" />
因此,当点击按钮时,我试图在搜索框中获取文本,并向屏幕发出警报

需要注意的一点是,页面上可能有多个带有“searchpersontxtbox”类的搜索框

所以我想从按钮正上方的文本框中获取文本

有人能帮我吗

$(function(){
    $('button').click(function() {
    var text = $('textarea').val();
    alert(button);
    }
});
像这样的?使用jQuery搜索获取textarea值,然后在变量中应用该方法并警告它的文本。应该有用吗

像这样的?使用jQuery搜索获取textarea值,然后在变量中应用该方法并警告它的文本。应该有效吗?

这不是有效的方法。在看不到其余标记的情况下,一种方法是:

$(document).ready(function () {
    $('.findpersonbtn').click(function () {
        var findpersonbutton = $(this)
            , searchbox = $(this).prev();

        alert(searchbox.val());
    });
});
您可以使用获取单击的
上方的元素。findpersonbtn

这不是工作方式。在看不到其余标记的情况下,一种方法是:

$(document).ready(function () {
    $('.findpersonbtn').click(function () {
        var findpersonbutton = $(this)
            , searchbox = $(this).prev();

        alert(searchbox.val());
    });
});

您可以使用来获取单击的
.findpersonbtn

上方的元素。您可能希望签出
.previousSibling()
,它将返回以前的输入


您可能希望签出
.previousSibling()
,它将返回以前的输入


我想他在找相关的东西。。。像这样

$(document).ready(function(){ 

$('.findpersonbtn ').click(function () {
     var findpersonbutton = $(this);

//do relative search here...
     var searchbox = findpersonbutton.parent().find('.searchpersontxtbox');
     alert(searchbox.val());

});

}))

我想他在找相对的东西。。。像这样

$(document).ready(function(){ 

$('.findpersonbtn ').click(function () {
     var findpersonbutton = $(this);

//do relative search here...
     var searchbox = findpersonbutton.parent().find('.searchpersontxtbox');
     alert(searchbox.val());

});

}))

可能是$(this.prev('.searchpersontxtbox');为了安全起见。可能是$(this.prev('.searchpersontxtbox');只是为了安全。呃…按钮被禁用。呃…按钮被禁用。