简体   繁体   English

Laravel \\ Blade-获取动态添加的选定单选按钮的值

[英]Laravel\Blade - get value of selected radio button dynamically added

I'm developing a questionnaire in Laravel. 我正在Laravel中制作问卷。 The questions and their answers are printed throw a foreach. 这些问题及其答案被打印出来。 Now I need to retrieve the selected radio/answer for each question in order to insert each couple in an indexed array. 现在,我需要为每个问题检索选定的单选/答案,以便将每个对插入索引数组中。 The problem is that the name of the html tag radio is dynamic and not static so how can I retrieve the answer for each question? 问题在于html标记单选的名称是动态的,而不是静态的,因此我如何检索每个问题的答案?

In the code, {{$answer[0]}} is the id of the answer, while {{$answer[1]}} the text of the answer 在代码中,{{$ answer [0]}}是答案的ID,而{{$ answer [1]}}是答案的文本

@foreach ($question->answers as $answer)

   <div class ="radio">
      <label>
         <input type="radio" name={{$question->id_question}} value= {{$answer[0]}}>
         {{$answer[1]}}
      </label>
   </div> 
@endif
@endforeach 

Please have a look at this, if it's what you're looking for: 如果您正在寻找它,请看一下它:

 <div id="container">

    <div class ="radio01">
      <label for="question1">question1</label><input type="radio" name="question1"  id="question1" value="answer1">
      <label for="question1">question1</label><input type="radio" name="question1"  id="question1" value="answer2">
      <label for="question1">question1</label><input type="radio" name="question1"  id="question1" value="answer3">
      <label for="question1">question1</label><input type="radio" name="question1"  id="question1" value="answer4">
      <label for="question1">question1</label><input type="radio" name="question1"  id="question1" value="answer5">

   </div> 
    <div class ="radio02">
      <label for="question2">question2</label><input type="radio" name="question2"  id="question2" value="answer1">
      <label for="question2">question2</label><input type="radio" name="question2"  id="question2" value="answer2">
      <label for="question2">question2</label><input type="radio" name="question2" id="question2" value="answer3">
      <label for="question2">question2</label><input type="radio" name="question2"  id="question2" value="answer4">
      <label for="question2">question2</label><input type="radio" name="question2"  id="question2" value="answer5">

   </div> 

.... ....

<script>
    var selectedAnswers = function () {

    var answers = [];

    $('input:radio').each(function () {

        var $this = $(this),

            name = $this.attr('name');
            value = $this.attr('value');

        if ($(this).prop('checked')) {
            answers.push({"name":name, "value":value});
        }

    });

    return answers;

};

$('#container').on('change', ':radio', function() { console.log(selectedAnswers()); }); 

</script>

Also your html is not "optimal" missing double quotes and correct hierarchy: 另外,您的html也不是缺少双引号和正确层次结构的“最佳”选项:

   <label>
     <input type="radio" name={{$question->id_question}} value= {{$answer[0]}}>
     {{$answer[1]}}
  </label>

Should be (like in my example above): 应该是(就像上面的例子一样):

     <input type="radio" name="{{$question->id_question}}"  id="{{$question->id_question}}" value=" {{$answer[0]}}">
     <label for="{{$question->id_question}}">{{$answer[1]}}</label>

Function given below to get values of all input radio. 下面给出的函数用于获取所有输入无线电的值。

function get_answers() {
    var answers = {};
    $.each($("input:radio"), function () {
        answers[$(this).attr("name")] = $(this).val();
    });
    return answers;
}

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

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