jquery获取以前的输入文本值

时间:2016-04-07 09:58:26

标签: javascript jquery html

我的表单中有以下html:

    <div>
    <input type="text" style="margin-top: 10px; float: none;" class="validate[required] span3 m-wrap answopts answerdets" placeholder="Answer Options" id="answeroptions" name="answeroptions[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="answeroptions1" name="answeroptions[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="answeroptions2" name="answeroptions[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()。prev()。val()但它没有用。任何帮助将不胜感激。在此先感谢。

4 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:1)

在使用.closest()找到.parent()之前,您可以使用radio.prev()首先选择所点击的div的父级。您还需要使用.find()找到input元素。

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

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

<强>更新

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

&#13;
&#13;
$(function() {
    $('p > :radio').on('change', function() {
        var ival = $(this).closest('p').prev().find('input').val();
        alert( ival );
    });
});
&#13;
<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="answeroptions2" name="answeroptions[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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如你所说,你有多个div和p标签元素,这导致单选按钮和输入的重复。 ID必须是唯一的。你可以宁愿使用同一个班级。

对于遍历,您可以遍历到最近的p标记,然后遍历其先前的兄弟div并在其中找到元素:

$('.disoptinputs :radio').click(function(){
   var previnp = $(this).closest('.disoptinputs').prev().find('input').val();
});

答案 3 :(得分:0)

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

尝试上面的代码,看看它是否正常工作:)

$(this)以当前元素为目标。 (单选按钮)

parent()定位当前元素的父元素(此处为P标记)

prev()定位P tag(div)的前一个元素

find("input[type=text]")定位prev div

中的输入标记
相关问题