JS - 在函数内时多次执行键

JS - keydown execute multiple times when is inside a function

本文关键字:执行 函数 JS      更新时间:2023-11-04
    <div class="comentPub" id="comentPub_<?php echo $pub_id;?>">
<div id="comentPub_content">
<img onclick="close_coment_box(<?php echo $pub_id; ?>)" src="imagens/x.png" style="width: 10px; height: 10px; float: right; margin-right: 10px;"/><br/>
<a href="perfil.php">
<img src="<?php echo $imageDirectory;?>" style="width: 25px; height: 25px;"> 
</a>
<textarea name="usercoment" class="comentBox" id="comentBox_<?php echo $pub_id;?>" placeholder="Escreve um coment&aacute;rio..." onkeypress="keypressed(this.id, <?php echo $userID;?>, <?php echo $pub_id;?>)"></textarea><br/

这是我要获取值并验证按键功能的文本区域

<li style="background: #ddd; font-size: 0; height: 1px; line-height: 0; width: 350px; float: left;"></li>
<?php
$comentarios = "SELECT * FROM comentPub WHERE pubID = '$pub_id'";
$comentarios_query = mysql_query($comentarios, $liga);
while ($row4 = mysql_fetch_array($comentarios_query)){
$userComentID = $row4['userID'];
$coment = $row4['coment'];
$comentID = $row4['comentID'];
$getUserComentData = "SELECT * FROM perfis WHERE id='$userComentID'";
                                    $getUserComentData_query = mysql_query($getUserComentData, $liga);
while ($row5 = mysql_fetch_array($getUserComentData_query)){
                                        $userComentImage = $row5['imagem'];
$nome = $row5['nome'].' '.$row5['apelidos'];
}
}
?>
</div>
</div>

^这是我的HTML页面,现在是我的JS函数

function keypressed(textareaID, comentUserID, pubID){
    var textareaID = "#" + textareaID;
    $(textareaID).keydown(function(e){ 
       if(e.shiftKey && e.keyCode == 13){ 
        var txt = $(textareaID);
            txt.val(txt.val());
       } else if (e.keyCode == 13) { 
          e.preventDefault();
          var userComent = $(textareaID).val(); 
          $.post("comentar_pub.php", {coment: userComent, user: comentUserID, pub: pubID});      
          $(textareaID).attr("value", "");
       }
    });
}

现在,如果你执行它,它将执行多次"$.POST("comentar_pub.php", {coment: userComent, user: comentUserID, pub: pubID}(;"并在数据库上插入多行。如果您尝试将"e.keyCode == 13"中的代码更改为"警报("1"(;警报("2"(;",您可以更好地看到效果。请帮忙

它是一个要跟踪的代码的地狱...无论如何,您的问题在于您在函数中的textarea + 中onkeypress触发代码,keypressed()将另一个事件侦听器$(textareaID).keydown()添加到同一元素......你可以看到这是怎么回事。

这里需要注意的是,jquery keydown添加事件侦听器,并且不会替换现有的键。因此,每次触发onkeypress时,您都会向 $(textareaID) 元素添加另一个$(textareaID).keydown()事件操作(传递给事件的匿名函数(。

每次按下键时都会添加一个事件侦听器,当按键触发时,该帖子将按onkeypresstextarea上触发的 X 次执行。

删除添加的$(textareaID).keydown()事件并运行其中的代码,您应该被设置...如果我正确理解这应该如何工作。

编辑:所以代码应该是

<textarea name="usercoment" class="comentBox" id="comentBox_<?php echo $pub_id;?>" placeholder="Escreve um coment&aacute;rio..." onkeypress="keypressed(event, this, <?php echo $userID;?>, <?php echo $pub_id;?>)"></textarea>

和 JS

function keypressed(e, textareaDOM, comentUserID, pubID){
    if (!e) var e = window.event;
    var txt = $(textareaDOM);
    if(e.shiftKey && e.keyCode == 13){ 
        txt.val(txt.val());
    } else if (e.keyCode == 13) { 
        e.preventDefault();
        var userComent = txt.val(); 
        $.post("comentar_pub.php", {coment: userComent, user: comentUserID, pub: pubID});      
        txt.attr("value", "");
    }
}

注意! - 因为keypressed函数是通过正常事件而不是jQuery事件触发的,所以我不记得e.shiftKey是否真的有效,如果我没记错的话,你仍然需要单独侦听shift。但我会把它留给你!