背景
使用VanillaJS获取HTML的action属性值。
代码
下面是代码和小提琴
<html>
<head>
<script type='text/javascript'>
function init() {
alert( document.forms['form-load'].action );
}
</script>
</head>
<body onload="init();">
<form name="form-load" id="form-load" action="http://google.com/q">
<input name="query" id="query" type="text" />
<button id="button-load" type="submit" name="action" value="load">Load</button>
</form>
</body>
</html>问题
对话框显示了[object HTMLButtonElement]中http://google.com/q的期望(使用Firefox32)。
问题
如何检索表单的action属性值?
更新
以下问题可能是相关的:
发布于 2014-09-18 01:15:05
提供与标准表单属性或方法名称冲突的表单元素名称是个坏主意。例如,避免给元素命名如下:submit、method、action、reset。
如果必须使用.getAttribute()方法,则使用以下方法获取属性值:
function init() {
alert( document.forms['form-load'].getAttribute("action") );
}发布于 2014-09-18 01:25:03
我认为将属性name命名为" name“是一种不好的做法。为什么不使用id呢?
function init() {
alert( "By ID:" document.getElementById('form-load').getAttribute('action') );
}https://stackoverflow.com/questions/25902647
复制相似问题