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?

dpftppc9674
dpftppc9674 库克,是控制台可以输出对象{rcomment=“你的评论太短了”}。
6 年多之前 回复
dsfew215211
dsfew215211 我把console.log(response.error);在$.each()调用之前,确认数据是您期望的格式(并且执行到达该行)。
6 年多之前 回复
dtgr6303
dtgr6303 验证客户端。不是通过ajax等待请求,而是创建在发送之前显示跨度的脚本。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐