javascript - 按类获取文本区域并动态设置名称

标签 javascript php jquery html

我真的不知道为什么我不能做这明显愚蠢的事情。

我有一个模板,可以动态解析带有仅带有 class 属性的 html 文本区域的 js 文件。

我想要做的是向它添加 name 属性,这样我就可以在 php 中使用 $_POST 获取它。

到目前为止我已经尝试过:

var txt = $('.note-codable');
txt.prop('name','description');

$('.note-codable').attr('name','description');

以及其他似乎不起作用的选项。

这是动态添加的 html:

<div class="note-editor">
  //other divs
  <textarea class="note-codable"></textarea>
</div>

当我这样做时(为了尝试代码):

var txt = $('.note-codable');
alert(txt);

结果是[对象][对象]

我错过了什么?为什么attr name不写?

最佳答案

试试这个,告诉我是否还有什么我可以做的。

window.onload = function(){
  //get the element
  var txt = $('.note-codable');
  //set the name attribute
  txt.name = 'yourName';
  
  //get the name and console.log it
  console.log(txt.name);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="note-editor">
  //other divs
  <textarea class="note-codable"></textarea>
</div>

关于javascript - 按类获取文本区域并动态设置名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748396/

相关文章:

javascript - Javascript 与 Haskell 中的 Eratosthenes 筛选

javascript - 我无法通过 AJAX 获取嵌套 JSON 结果

php - 在PHP中异步处理多个GET请求

javascript - 页面加载时推特框的 jQuery CSS 高度

android - :active not working on phonegap

javascript - res.render sweetalert 在 ejs 文件上

javascript - 如何使用 python 删除 &lt;script&gt; 和 &lt;/script&gt; 之间的文本?

php - 如何通过 Laravel 更新外部样式表中的 CSS 属性?

php - 修改php oop mysql查询

javascript - 未使用 mocha 定义 jQuery 以测试 wordpress javascript