dongniaoli1822 2014-12-19 12:44
浏览 33

如何把php字符串jQuery验证错误信息

I'm using jquery validation to validate a form the js code looks like this:

form.validate({
        messages: {
            years_of_experience: {
                range: "<?php echo Translation::tr('profileform.error.yearsOfExperience');?>"
            }
        },
        rules: {
            years_of_experience: {
                range: [0, 100]
            }
        }
});     

Translation::tr('profileform.error.yearsOfExperience') -> makes a simple mysql call to the database and returns a string (the error message for the element) but the problem is that i can't display the error message (i get blank).

if i change the javasctipt code like this it works just fine:

form.validate({
        messages: {
            years_of_experience: {
                range: "Please select a number from 0 to 100"
            }
        },
        rules: {
            years_of_experience: {
                range: [0, 100]
            }
        }
});  

Can you help me ?

Thanks

  • 写回答

2条回答 默认 最新

  • duandaiqin6080 2014-12-19 13:14
    关注

    The quotes around the php code tell javascsript it's just a string nothing is interpreting it. Try

        form.validate({
            messages: {
                years_of_experience: {
                    range: <?php echo json_encode( Translation::tr('profileform.error.yearsOfExperience') ); ?>;
                }
            },
            rules: {
                years_of_experience: {
                    range: [0, 100]
                }
            }
       });  
    

    It's ugly... but that should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大