jquery获取先前输入的文本值

jquery get previous input text value

本文关键字:文本 输入 获取 jquery      更新时间:2024-02-24

我的表单中有以下html:

    <div>
    <input type="text" style="margin-top: 10px; float: none;" class="validate[required] span3 m-wrap answopts answerdets" placeholder="Answer Options" id="answersweroptions" name="answersweroptions[0]">
    <img class="removeanswer" style="width: 50px; margin-top: 9px;" src="../images/remove.png">
</div>
<p class="disoptinputs">
    <input type="radio" style="margin:-85px 0 0 !important;" class="validate[required] coranschk"  id="questionoption" name="questionoption[0][]">
</p>

<div>
    <input type="text" style="margin-top: 10px; float: none;" class="validate[required] span3 m-wrap answopts answerdets0" placeholder="Answer Options" id="answersweroptions1" name="answersweroptions[0][]">
    <img id="1" class="removeanswer" style="width: 50px; margin-top: 9px;" src="../images/remove.png"></div>
<p class="disoptinputs0">
    <input type="radio" class="coranschk validate[required]" cntr="7" atrid="0" id="questionoption7" name="questionoption[0][]">
</p>
<div>
    <input type="text" style="margin-top: 10px; float: none;" class="validate[required] span3 m-wrap answopts answerdets0" placeholder="Answer Options" id="answersweroptions2" name="answersweroptions[0][]">
    <img id="2" class="removeanswer" style="width: 50px; margin-top: 9px;" src="../images/remove.png">
</div>
<p class="disoptinputs0">
    <input type="radio" class="coranschk validate[required]" cntr="7" atrid="0" id="questionoption7" name="questionoption[0][]">
</p>

这个div和p标记是多个。现在,我想要的是,当用户点击任何单选按钮时,我想要div中单选按钮之前的输入框的值。我试过这个$(this).prev().prve().val(),但它不起作用。任何帮助都将不胜感激。提前感谢。

您可以使用.closest().parent()首先选择单击的radio的父级,然后使用.prev()定位div。您还需要使用.find()来定位input元素。

$(this).closest('.disoptinputs').prev().find('input').val();

注意:由于有多个输入元素,您不需要id,因为它们应该是唯一的。

更新

如果p元素没有公共类,则此代码将不起作用。在这种情况下,您必须将.disoptinputs替换为p,如下面的演示所示。

$(function() {
    $('p > :radio').on('change', function() {
        var ival = $(this).closest('p').prev().find('input').val();
        alert( ival );
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <input type="text" style="margin-top: 10px; float: none;" class="validate[required] span3 m-wrap answopts answerdets0" placeholder="Answer Options" id="answersweroptions2" name="answersweroptions[0][]">
    <img id="2" class="removeanswer" style="width: 50px; margin-top: 9px;" src="../images/remove.png">
</div>
<p class="disoptinputs0">
    <input type="radio" class="coranschk validate[required]" cntr="7" atrid="0" id="questionoption7" name="questionoption[0][]">
</p>

尝试:

$(this).parent().prev().find('input').val()

正如您所说,您有多个类似的div和p标记元素,这会导致单选按钮和输入的id重复。ID必须是唯一的。你们可以使用相同的类。

对于遍历,您可以遍历到最近的p标记,然后遍历到它的上一个同级div,并在其中找到元素:

$('.disoptinputs :radio').click(function(){
   var previnp = $(this).closest('.disoptinputs').prev().find('input').val();
});
$('input[type="radio"]').click(function() {
   var getPrevText = $(this).parent().prev().find("input[type=text]:first").val();
   alert(getPrevText); //returns with the prev Val
});

尝试上面的代码,看看它的工作:)

CCD_ 12到目标电流元件。(单选按钮)

parent()到目标当前元素的父元素(此处为p标签)

prev()以p标签(div)的前一个元素为目标

find("input[type=text]")到前一个div 内的目标输入标签