我想将form.find元素传递给我的函数AvailabilityChecker。我试图获取元素属性但没有成功。怎么了?
var AvailabilityChecker = function (ev, el) {
console.log("arguments: ",Array.prototype.slice.call(arguments)); //gives: arguments: Array [ Object ]
console.log(ev);
console.log("AvailabilityChecker: ",el.attr('AvailabilityChecker') //gives Typeerror
}
form.find("input[AvailabilityChecker]").keyup(AvailabilityChecker.bind(this));
答案 0 :(得分:2)
el
不会成为神奇地附加为参数的属性。所以你需要以另一种方式做到这一点。使用.bind()也会改变如何获取元素引用
var AvailabilityChecker = function (ev) {
console.log("1-DOM", ev.target.getAttribute("AvailabilityChecker"))
console.log("1-JQ",$(ev.target).attr("AvailabilityChecker"))
}
$("input[AvailabilityChecker]").keyup(AvailabilityChecker.bind(this));
var AvailabilityChecker2 = function (ev) {
console.log("2-DOM", this.getAttribute("AvailabilityChecker"))
console.log("2-JQ",$(this).attr("AvailabilityChecker"))
}
$("input[AvailabilityChecker]").keyup(AvailabilityChecker2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input AvailabilityChecker="aaaa">