doukai2839 2014-04-02 23:44
浏览 83

Jquery验证错误未在<span>正文中显示

I have a form with validation which will display error in <span class="error" id="rcomment_error"></span>, somehow I cannot get my errors display if validation failed.

Here I shorthen all the codes.

ajax response if validation failed:

$.ajax({
url: 'request.php',
type: 'POST',
data: params,
dataType: 'json',
success: function(response){
    if(response.success == 'success'){
        alert('done');

    }else{
        //if validation failed
        $('[id$="_error"]').html('');
        $.each(response.error, function(key, value){
            if(value){
                $('#' + key + '_error').html(value);
            }
        });
    }
},

request.php

// validate input fields
if(empty($rname) || strlen($rname) < 2){
$message['error']['rname'] = 'Enter your name';
}
if(empty($rcomment)){
$message['error']['rcomment'] = 'Your comment is too short';
}

echo json_encode($message);

validation error will display between <span> literally, below two line of codes are included as part of a file called loadData.php, when I go to page product.php, loadData.php will request by ajax and fetched all necessary data and load into <div id="product_list"></div>, I was speculated this is the reason why the error message cannot be output with callback data.

<input type="text" name="rname"><span class="error" id="rname_error"></span>
<textarea rows="3" name="rcomment"></textarea><span class="error" id="rcomment_error"></span>

I tried to put <span class="error" id="rname_error"></span><span class="error" id="rcomment_error"> in product.php and validate again, this time the errors are shown properly, why is it so?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器