简体   繁体   English

如何添加元素旁边的输入文本字段的值?

[英]how to add value of input textfield that is next to an element?

I have a dynamic form that is generated by php. 我有一个由php生成的动态表格。

  print '<div class="choices">';
    print '<input type="radio" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>' . $row['answer1'] .'<br/>';
    print '<input type="hidden" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>';

    print '<input type="radio" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="2"/>' . $row['answer2'] .'<br/>';
    print '<input type="hidden" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>';

    print '<input type="radio" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="3"/>' . $row['answer3'] .'<br/>';
    print '<input type="hidden" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>';

    print '<input type="radio" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="4"/>' . $row['answer4'] .'<br/>';
    print '<input type="hidden" class="answer' . $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>';

    print '<input type="hidden" class="your" id="answer" name="correctanswer'. $q . '" value="0"/>';

    print '</div>';

I want to change the value of input type hidden which has a class of your that is next to div .choices . 我想改变隐藏输入类型具有一流的价值your接下来将div的.choices I used the jQuery below. 我使用下面的jQuery。

$('input[type=radio]').on('click', function(){
//  alert(this.value);
    $(".choices ~ .answer").val(this.value);

});

But it is not working. 但这是行不通的。 Any suggestions? 有什么建议么?

EDIT: 编辑:

HTML 的HTML

First of all, you have the same ID on multiple elements. 首先,您在多个元素上具有相同的ID。 IDs should be unique. ID应该是唯一的。 I'm unclear as to where input.your is located, your description says it's a sibling of div.choices, but the code indicates it's a child of div.choices... 我不清楚input.your的位置,您的描述是div.choices的同级兄弟,但是代码表明它是div.choices的子代...

If input.your is a sibling of div.choices: 如果input.your是div.choices的同级兄弟:

    $('input[type=radio]').on('click', function(){
        $(this).parent().siblings('.your').val($(this).val());
    });

If input.your is a child of div.choices: 如果input.your是div.choices的子级:

    $('input[type=radio]').on('click', function(){
        $(this).siblings('.your').val($(this).val());
    });

EDIT: misunderstood question 编辑:被误解的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM