dqpc1845 2019-07-29 02:06
浏览 51

如何删除插入jquery的errorPlacement中的元素

I made a survey form with html, php and yii2. I have used the jquery validation plugin to check whether each question has an answer. Now the validation works and an error message pops up when a question is not answered.

I have also added a "br" tag to change line so that the error message will not be in the same row of the radio buttons. Now when I select an answer the error message disappears, but the "br" tag still exists. So how do I remove the "br" tag tag as well?

The following are my codes:

<?php

foreach ($questions as $question) {
    ?>

    <label class="question"><?php echo $question->getTitle(); ?></label><br>

    <?php
    foreach ($choices as $choice) {
        ?>

        <label class="radio-inline"><input type="radio" name="<?php echo $question->id . 'radio'; ?>"
                                           value="<?php echo $choice->id; ?>"><?php echo $choice->getTitle(); ?></label>

    <?php }

    $this->registerJs('
                    rule[ "' . $question->id . 'radio"] = {
                            required: true,
                            }

                ', View::POS_BEGIN);
    ?>

<?php } ?>

<?php
$this->registerJs('

                    $(document).ready(function(){

                    $("#testForm").validate({
                    rules: rule,
                    errorPlacement: function (error, element) {
                        if (element.attr("type") == "checkbox" || element.attr("type") == "radio") {
                            error.insertAfter($(element).parents("label").prev($(".question"))); 
                            $("<br>").insertAfter($(element).parents("label").prev($(".question"))); 
                        }

                        else{
                            error.insertAfter(element);
                        }

                    }

                    });
                });

                ',
    View::POS_END);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题