Php Can';t获取jQuery';s.replaceWith()是否正常工作?

Php Can';t获取jQuery';s.replaceWith()是否正常工作?,php,javascript,jquery,Php,Javascript,Jquery,基本上,我试图让jQuery在单击后用表单和状态div替换按钮 我是jQuery新手,几乎不是高级程序员,所以我相信解决方案会相对简单。另一方面,我对这个问题束手无策,任何帮助都将不胜感激。多谢各位 编辑:找到解决方案。有趣的是,只有当我把它放在一个单独的.js文件中并引用它时,它才起作用,而不是当我在php页面的标签中有它时。再次感谢所有回应的人 $(文档).ready(函数(){ $(“#repbutton”)。单击(函数(){ $(“#repbutton”)。替换为(” 对他们的回复给予

基本上,我试图让jQuery在单击后用表单和状态div替换按钮

我是jQuery新手,几乎不是高级程序员,所以我相信解决方案会相对简单。另一方面,我对这个问题束手无策,任何帮助都将不胜感激。多谢各位

编辑:找到解决方案。有趣的是,只有当我把它放在一个单独的.js文件中并引用它时,它才起作用,而不是当我在php页面的标签中有它时。再次感谢所有回应的人


$(文档).ready(函数(){
$(“#repbutton”)。单击(函数(){
$(“#repbutton”)。替换为(”
对他们的回复给予积极的评价?
评论:
尝试使用
append()
prepend()
html()
函数来操作DOM。这样做可能会让您感到困惑并遇到各种问题

你应该能够

  • 隐藏按钮
  • 将表单的HTML附加到按钮所在的元素
然后在完成表单后轻松地执行以下操作

  • 再次显示按钮
  • 删除表单

除非使用
\
转义换行符,否则换行符在字符串文本中无效。简单地说,将字符串压缩到一行或在每行末尾添加
\

$(document).ready(function() { 
   $("#repbutton").click(function() {
   $("#repbutton").replaceWith("<form id=\"reputation_form\" method=\"post\">\
    Give positive reputation to <?=$reply_user['username']?> for their reply?\
    Comment: <input type=\"text\" name=\"comment\" id=\"comment\" /> \
             <input type=\"hidden\" id=\"uid\" value=\"<?=$reply_user['id']?>\">\
             <input type=\"hidden\" id=\"rid\" value=\"<?=$replies_row['id']?>\">\
             <input type=\"hidden\" id=\"urid\" value=\"<?=$user_row['id']?>\">\
             <input type=\"submit\" value=\"Give Reputation\" />\
</form>\
<div id=\"status\">\
        <p></p>\
    </div>");

    });
});​
$(文档).ready(函数(){
$(“#repbutton”)。单击(函数(){
$(“#repbutton”)。替换为(”\
对他们的回复给予积极的评价\
评论:\

为什么不在单击时隐藏按钮并显示表单?

在JavaScript中,字符串中不能有这样的返回语句,您需要在每行末尾使用
\
将其声明为多行字符串,如下所示:

$(document).ready(function() { 
   $("#repbutton").click(function() {
   $("#repbutton").replaceWith("<form id=\"reputation_form\" method=\"post\"> \
    Give positive reputation to <?=$reply_user['username']?> for their reply? \
    Comment: <input type=\"text\" name=\"comment\" id=\"comment\" /> \
             <input type=\"hidden\" id=\"uid\" value=\"<?=$reply_user['id']?>\"> \
             <input type=\"hidden\" id=\"rid\" value=\"<?=$replies_row['id']?>\"> \
             <input type=\"hidden\" id=\"urid\" value=\"<?=$user_row['id']?>\"> \
             <input type=\"submit\" value=\"Give Reputation\" /> \
</form> \
<div id=\"status\"> \
        <p></p> \
    </div>"); 

    });
});​
$(文档).ready(函数(){
$(“#repbutton”)。单击(函数(){
$(“#repbutton”)。替换为(”\
对他们的回复给予积极的评价\
评论:\

非常感谢-这似乎解决了我遇到的问题。我最终选择了“\”解决方案,因为如果我必须回到它,那么编辑它会更好。看着它,你发布了与最终解决它的解决方案相同的解决方案。谢谢!
$(document).ready(function() { 
   $("#repbutton").click(function() {
   $("#repbutton").replaceWith("<form id=\"reputation_form\" method=\"post\"> \
    Give positive reputation to <?=$reply_user['username']?> for their reply? \
    Comment: <input type=\"text\" name=\"comment\" id=\"comment\" /> \
             <input type=\"hidden\" id=\"uid\" value=\"<?=$reply_user['id']?>\"> \
             <input type=\"hidden\" id=\"rid\" value=\"<?=$replies_row['id']?>\"> \
             <input type=\"hidden\" id=\"urid\" value=\"<?=$user_row['id']?>\"> \
             <input type=\"submit\" value=\"Give Reputation\" /> \
</form> \
<div id=\"status\"> \
        <p></p> \
    </div>"); 

    });
});​