根据使用 ajax php mysql 选择的选项验证调查问卷中的相应文本区域


Validating respective textarea in a questionnaire based on the option selected using ajax php mysql

首先,提前感谢,

我正在设计一个问卷,其中包含3-5个单选按钮和每个问题的一个评论框。某些选项是强制性的,因此用户必须填写一些评论,否则他无法填写其他问题。单选按钮和文本区域的名称都是question_id,单选按钮的值是选中的值。名称存储为数组,以便在提交表单时插入到数据库中。输入字段为

<input type='radio' name='ans_val[<?php echo $qid; ?>]' value='<?php echo $gans; ?>' />    
<textarea rows='3' name='cmnt[<?php echo $qid; ?>]' id="req"></textarea>

我使用 ajax 来查找所选选项是否是必需的,因为这些选项存储在数据库中。我能够在选择一个选项时查看传递的值以及执行以检查强制选项的查询结果。我使用的 ajax 代码是

$(document).ready(function () {    
        $('input:radio').click(function(){    
            var ans_id = $(this).attr('value');    
            var q_id = $(this).attr('name');    
            var dataString = 'id='+ans_id;    
            $.ajax({    
                type: "POST",    
                data: dataString,    
                url: "answers_chq.php",    
                success: function(data){    
                    $('body').append(dataString);    
                   // $('textarea').html(dataString);    
                }    
            });    
        });    
    });    

在ans_chq.php中,当用户选择强制选项时,"$mandatory"将变为"1",否则变为"0"。

我想知道,如何将变量$mandatory从另一个页面返回到当前页面,以及如何根据所选选项验证相应的文本区域......

请帮我这个...

如果每个字段都有单独的id,请执行以下操作:

    $("#elementId").prop("required", "true");// here elementId is id of your input field.and id should be distinct for each input

如果每个字段都有类,请执行以下操作:

  $(".elementId").prop("required", "true");// here elementId is class of your input field.and class need not to be unique.

如果您使用带有名称的输入字段,请尝试以下操作:

  $("input[name='q_id']").prop("required", "true");// here q_id is name of the input field.