问题描述
|
我有一个实时预览表:http://davidwalsh.name/jquery-comment-preview
它使用jquery实时显示预览:
$(document).ready(function() {
$(\'#live-preview-form input,#live-preview-form textarea\').bind(\'blur keyup\',function() {
$(\'#lp-comment\').text($(\'#comment\').val());
$(\'#lp-comment\').html($(\'#lp-comment\').html().replace(/\\n/g,\'<br />\'));
});
});
如果我需要在来自textarea的\“#comment \”上应用一些PHP函数,然后将其放入jquery验证中怎么办?
解决方法
如果要应用PHP函数,则必须使用jquery ajax(将值发布到php页面并获取返回的结果)。
例如:
process.php:
<?php
function my_function($in){
....
return $out;
}
$result = my_function($_POST[\'q\']);
echo json_encode($result);
?>
js:
$(document).ready(function() {
$(\'#live-preview-form input,#live-preview-form textarea\').bind(\'blur keyup\',function() {
$.ajax({type: \"POST\",url: \"function.php\",data:{ \"q\": $(\'#comment\').val()},dataType: \"json\",success: function(result){
$(\'#lp-comment\').text(result);
$(\'#lp-comment\').html($(\'#lp-comment\').html().replace(/\\n/g,\'<br />\'));
}});
});
});
但是,不建议这样做,因为用户每次键入单个字符时都会查询您的服务器。
最好的方法是通过javascript函数而不是PHP函数。
,我认为您应该使用此处提供的jquery表单验证插件。它处理您需要的所有基本表单验证。