簡體   English   中英

我想從使用onkeypress事件的textarea獲取一個屬性

[英]I want to get an attribute from the textarea that uses the onkeypress event

我有以下代碼:

<div>
    <form name="comentario" id="<?php echo $id?>" action="funcion_enviar_comentario.php" method="post">
        <input hidden="hidden" type="text" id="inputid" name="inputid" value="<?php echo $id?>">
        <textarea  class=estilotextarea1 id="comentario<?php echo $id?>" onkeypress="process1(event,this)" name="<?php echo $id?>" placeholder="Introduce tu comentario aquí..."></textarea>
        <script>
            function process1(e) {
              //var ide= document.getElementById("comentario <?php echo $id?>").name;
              //var submit= document.getElementById("enviado<?php echo $id?>").
              var code = (e.keyCode ? e.keyCode : e.which);
              if (code == 13) { 
                $('form#comentario<?php echo $id?>').submit();

               }
            }
        </script>
    </form>       
</div>

我希望函數process1從觸發事件的文本區域中獲取屬性名稱(此代碼在查詢時在PHP內)將其存儲在變量中,因此我可以使用該變量來提交ID與變量匹配的表單。 提前致謝。

如果我理解正確,則可以使用以下方法:

<div>
                <form name="comentario" id="1" action="funcion_enviar_comentario.php" method="post">
                <input hidden="hidden" type="text" id="inputid" name="inputid" value="1">
                <textarea  class=estilotextarea1 id="comentario1" onkeypress="process1(event,this.id)" name="1" placeholder="Introduce tu comentario aquí..."></textarea>

            </form>       
</div>

 <div>
                <form name="comentario" id="2" action="funcion_enviar_comentario.php" method="post">
                <input hidden="hidden" type="text" id="inputid" name="inputid" value="2">
                <textarea  class=estilotextarea1 id="comentario2" onkeypress="process1(event,this.id)" name="1" placeholder="Introduce tu comentario aquí..."></textarea>

            </form>       
</div>   

JQuery的:

  function process1(e,id) {

          var code = (e.keyCode ? e.keyCode : e.which);
          if (code == 13) { 

                    alert('form #'+id);

           $('form #'+id).parent('form').submit();

            }
          }

因此,process1不應在循環內,您可以通過將其作為參數傳遞給函數來輕松獲取id……DEMO: http : //jsfiddle.net/msgosoaL/1/

而且,最后一行說明-您無法提交文本區域,因此您需要定位父表格...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM