douxuanjie2692 2019-04-01 06:47
浏览 87
已采纳

如何jquery验证动态生成的隐藏textarea

I am trying to validate multiple array of textarea which are added dynamically and are hidden as well. So what I am doing exactly is, I am using contenteditable div and whenever user inputs it sets the value of textarea which is hidden. So I am using validation for the textarea

I researched and found out about .each function and used it. But still it didn't work.

<div class="input-group">  
<textarea name="quiz[ques][0][ques]" style="display:none;" class="content-hidden">asd</textarea>            
<div contenteditable="true" id="question-edit-1" placeholder="Enter Question 1" name="quiz[question][]" class="content-visible valid" aria-invalid="false"></div>   
</div>

<div class="input-group">  
<textarea name="quiz[ques][1][ques]" style="display:none;" class="content-hidden">asd</textarea>            
<div contenteditable="true" id="question-edit-2" placeholder="Enter Question 2" name="quiz[question][]" class="content-visible valid" aria-invalid="false"></div>     
</div>

SCRIPT

$('form').submit(function (e) {
var ques = $('name^="quiz[ques]"');
ques.each(function() {
    $(this).rules("add", {
        required: true,
        messages: {
             required: "Please enter the questioon"
        }
    });
});
});
  • 写回答

1条回答 默认 最新

  • duanchuang3182 2019-04-01 15:56
    关注

    Your code...

    $('form').submit(function (e) {
        var ques = $('name^="quiz[ques]"');
        ques.each(function() {
            $(this).rules("add", {
                ....
            });
        });
    });
    

    You are not supposed to add rules when you submit the form, otherwise, you cannot expect to validate fields before the submit button is clicked. You would add rules when you dynamically create the new fields that need the rules. So remove the .rules() method from the submit handler and put in inside whatever function creates these new fields.

    Dynamically create the new field(s) -> then immediately add the rule(s)


    Secondly, your selector is totally broken. You are missing the outside brackets. Correct format for an "attribute starts with" selector is $('[attribute="value"]')

    So name attribute starts with quiz[ques] would look like this...

    $('[name^="quiz[ques]"]')
    

    If you need to be more specific with the selector, you could add the textarea element...

    $('textarea[name^="quiz[ques]"]')
    

    Finally, by default, all hidden fields are ignored (not validated). It's unclear why you would need to validate textarea elements if they remain hidden because the user could never interact with them. You can add/remove rules from hidden fields and they will automatically validate when shown and not validate when hidden.

    If you need to validate any fields that remain hidden, you will ned to override the default ignore settings. [] means "nothing". So ignore: [] means ignore nothing (validate everything).

    $('#myform').validate({
        ignore: [], // ignore nothing - validate everything
        // other settings....
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置