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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)