我需要获取表单名称的值。在此示例中,“basic_form”:
<form name="basic_form" method="post" action="/">
试过这个,但它不起作用:
function() {
var element=document.querySelector('form[name]').textContent;
return element;
}
答案 0 :(得分:1)
return element.getAttribute("name");
答案 1 :(得分:1)
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
/* or */
import has from 'has'; // https://www.npmjs.com/package/has
// ...
console.log(has.call(object, key));
function demo() {
var element=document.getElementById("myForm").name;
document.getElementById("get-name").innerHTML=element;
}
#get-name{
cursor: pointer;
}
答案 2 :(得分:0)
使用getAttribute
var element=document.querySelector('form[name]').getAttribute("name");
这是工作的jsFiddle:http://jsfiddle.net/QLdJW/470/