jQuery函数无法正常工作

时间:2011-08-17 21:03:47

标签: php javascript jquery

我编写了以下用于保存文档的JavaScript代码:

function save()
{
$.prompt('Are you sure you want to save changes to this document?',{
  callback: savechanges,
  buttons: { Yes: 'Yes', No: 'No' }
});
function savechanges(v,m){
 document.write('Date hello: ', page);
  if(v=="Yes")
  {
    var content = $('#content').val();// This line is not running
    var page = trim($('#pageid').val()); // This line is not running
    content = escape(content);
    $.post("../gamescripts/save.php",
    {  page: page, content: content },
    function(data){
    if(trim(data)=="success")
        {
       document.location = page ;                                   
        }
    }); 
  }
}
}

但是以下两个jQuery行无法从内容和页面ID中提取数据:但是,它们不会产生任何错误

    var content = $('#content').val();
    var page = trim($('#pageid').val());

这是调用JavaScript函数的php文件。我已经包含了jquery.js库。我是否需要包含任何其他库?

<script type="text/javascript" src="../gamejavascripts/jquery.js"></script>

<form id="form1" name="form1" method="post" action="javascript:save();">

<input name="pageid"  type="text" id="pageid"  value="<?php echo "$url"?>" READONLY/>
<textarea name="content" id= "content" cols="80" rows="18">";
<?php
$handle = fopen("$url","r");
 while(!feof($handle))
 { 
 $text = fgets($handle);
echo $text;
}
fclose($handle);
?>
</textarea>

<input type="submit" value="Save"  />

</form>

这是save.php文件

<?php

$content= $_POST["content"];
$page= trim($_POST["pageid"]);
$text = rawurldecode($content);



$myFile = $page;
$fh = fopen($myFile, 'w') or die("can't open file");

$status= "success";

fwrite($fh, $text);
fclose($fh);

echo $status;



?>

1 个答案:

答案 0 :(得分:1)

如果您正在寻找jQuery修剪功能,请使用$.trim()

var page = $.trim($('#pageid').val());//It will work now

你不应该在下面的行中出现任何错误

var content = $('#content').val();