【发布时间】:2016-04-12 04:06:38
【问题描述】:
我正在尝试使用here 的解决方案,使用它包含的link 提交form(而不是使用标准的input 提交按钮)
<form id="form-id">
<button id="your-id">submit</button>
</form>
var form = document.getElementById("form-id");
document.getElementById("your-id").addEventListener("click", function () {
form.submit();
});
表单是一个记录表,每个记录的最左侧列中都有一个“E”链接,用于加载记录以进行编辑
<td style="text-align:center;">
<a id="a_edit_btn_#RecordId#">E</a>
</td>
我想在点击时触发以下内容
document.getElementById( $("a[id^='a_edit_btn_']").get(0).getAttribute("id") ).addEventListener("click", function () {
...
form.submit();
});
但出现以下错误
Uncaught TypeError: Cannot read property 'getAttribute' of undefined
是否可以通过这种方式使用选择器来获取Id属性?
【问题讨论】:
-
你为什么要把
jQuery和普通的JS混在一起?坚持其中之一更容易,更安全 -
@nem 出于同样的原因,他们不付给我大笔钱。
-
这是……你很坏? :)(对不起)
-
如果您已经通过 jQuery 获取了该元素,那么使用
getElementById()再次获取它有什么意义?整件事毫无意义。 -
说真的,jQuery 只是为了简化 Javascript 的语法。您通过尝试奇怪地混合 jQuery 和纯 JS 使事情变得更加复杂。为什么不直接使用 jQuery 并使用它呢?这只会让您的生活更轻松。
标签: javascript jquery