我真的不知道为什么我不能做这明显愚蠢的事情。
我有一个模板,可以动态解析带有仅带有 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/