我正在尝试执行.blur()
,根据哪个标记丢失焦点来更改参数。但是,我希望能够在ajax请求中发送不同的对象,具体取决于id
丢失的焦点。作为jquery和javascript的新手,我不知道该怎么做。我可以构建两个相同但发送不同对象的函数,但我更喜欢构建一个。
代码:
function ajaxRequest(event, endpoint, payload, request) {
event.preventDefault();
$.ajax({
url: endpoint,
type: request,
data: payload,
success: function() {
console.log("test")
}
});
}
//pseudo code
$("#signup-email, #signup-username").blur(function(event) {
if (id == "signup-email") {
var payload = {"test1": 1};
} else if (id == "signup-username") {
var payload = {"test2": 0}
}
ajaxRequest(event, "auth/check_availability/", payload,"GET");
});
HTML:
<tr><th></th><td><input id="signup-username" maxlength="50" name="username" placeholder="Username" type="text" /></td></tr>
<tr><th></th><td><input id="signup-email" maxlength="50" name="email" placeholder="Email" type="email" /></td></tr>
答案 0 :(得分:1)
您永远不会设置id
。使用this.id
获取他们点击的元素的ID。
$("#signup-email, #signup-username").blur(function(event) {
if (this.id = "signup-email") {
var payload = {"test1": 1};
} else if (this.id = "signup-username") {
payload = {"test2": 0}
}
ajaxRequest(event, "auth/check_availability/", payload,"GET");
});