用jQuery获取textarea值不起作用

时间:2018-12-05 11:32:01

标签: jquery wordpress textarea

用val()读取不起作用,并且我只得到一个空结果。如何阅读用于使用Ajax更新数据库的文本区域?

$sql = "UPDATE ativos  SET ativo = ?, comentario = ?, data_aquisicao = ?, localizacao = ?, fabricante = ?, modelo = ?, imei = ?, 
        numero_serie = ?, ativo_sap = ?, anexo_a = ?, evento = ?, data_evento = ?, id_colaborador = ? WHERE id_ativo = ?";
        $sql2 = "SELECT id_colaborador FROM colaboradores WHERE nome = ?";
        $q = $pdo->prepare($sql,$sql2);

我的带有文本的html代码如下:

var k = jQuery.noConflict();
k(document).ready(function(){  
  k('.portfolio_classic_icon_content_middle').on('keyup', function(){
    var message = k('#gallery_images_comment').val();
    console.log(message);
  });
});

2 个答案:

答案 0 :(得分:2)

在使用k之前必须在$处定义等效位置。

var k = $;
k('.portfolio_classic_icon_content_middle').find('#gallery_images_comment').on('keyup', function(){
var message = k(this).val();
console.log(message);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="portfolio_classic_icon_content_middle">
  <textarea name="comment" id="gallery_images_comment" form="usrform">This is the text loaded</textarea>

</div>

答案 1 :(得分:0)

我不知道k的来源,但尝试改用$

$('.portfolio_classic_icon_content_middle').on('keyup', function(){
var message = $('#gallery_images_comment').val();
console.log(message);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="portfolio_classic_icon_content_middle">
    <textarea name="comment" id="gallery_images_comment" form="usrform">This is the text loaded</textarea>
</div>