javascript - 在 jQuery 中附加表单时出现问题

标签 javascript jquery html forms

这是我的代码:

$('.reply').click(function() {
    $leftwrap = $(this).parent();
    $comment = $(leftwrap).parent();
    $id = $comment.attr('id');
    if($(this).siblings('.reply-form').length == 0) { //check whether or not the form as been initiated yet
    $(this).parent().append('<?php $replytimestamp = strtotime("now"); ?>
    <form class="reply-form" action="/scripts/reply-process.php" method="post">
    <input type="hidden" name="replytimestamp" value="<?php echo $replytimestamp; ?>">
    <input type="hidden" name="replyto" value="'+ $id +'">
    <label for="name" class="reply-label">Name</label>
    <input type="text" name="name" class="reply-input">
    <label for="message" class="reply-label">Reply</label>
    <textarea name="message"class="reply-ta"></textarea>
    <input class="reply-submit" type="submit" name="submit" value="submit" />
    </form>'); 
    $(this).siblings('.reply-form').hide();
    $(this).siblings('.reply-form').slideDown();
} else  $(this).siblings('.reply-form').slideToggle(); //if it is, toggle it

}); 

当我点击“.reply”时,什么也没有发生,没有幻灯片,什么也没有。我在 Firebug 中找不到任何东西。那么知道怎么回事吗?谢谢!!

最佳答案

我建议始终在 if 语句中使用括号,但无论如何,您的问题就在这里:

$leftwrap = $(this).parent();
$comment = $(leftwrap).parent();

如果您打算这样做,则需要:

$leftwrap = $(this).parent();
$comment = $leftwrap.parent();

$ 变量并不神奇,它是变量名称的一部分,因此您必须包含它。

关于javascript - 在 jQuery 中附加表单时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1955952/

相关文章:

javascript - 使用 Javascript 或 jQuery 取消屏蔽文本框密码

javascript - 从模态对话框 : Javascript 访问父 Windows URL

javascript - 在每一行之后将 div 添加到表中

javascript - 使用 jquery 更改 html 表单元素占位符

javascript - 拆分日期范围重叠成 block (javascript)

javascript - 为什么这个函数返回数组中的所有对象?

jQuery - 比较值和颜色文本

javascript - 如何使用一些运算符组合两个数组?

javascript - JQuery 的 width() 不排除隐藏范围内元素的填充

javascript - 在 JS 中打破长行的最佳实践