首先,提前致谢,
我正在设计一份调查问卷,其中每个问题都有 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: "ans_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.
关于javascript - 根据使用 ajax php mysql 选择的选项验证调查问卷中的相应文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27419727/