我有2个输入字段
a)电子邮件和
b)private_email
我希望在用户双击电子邮件字段时显示警报消息1,当用户双击private_email字段时显示2。所以我使用以下jQuery代码,但它在警告消息中仅显示1:
jQuery代码:
$(document).ready(function(){
$("input#email, input#email_private").dblclick(function(){
var email = $("input#email").val();
var private_email = $("input#email_private").val();
var cdid = $("#cdid_hidden").val();
var cid=$('#cdid_hidden').val();
if(email) {
alert(1);
window.open('mailto:<?php echo $res['email']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);
}
var url = "response.php?confirmEmail=confirm&cdid="+cdid;
$.get(url);
getDetails(cid);
});
});
答案 0 :(得分:1)
只需检查点击is()
的元素是否相应,然后执行。
$(document).ready(function(){
$("input#email, input#email_private").dblclick(function(){
var email = $("input#email");
var emailVal = email.val();
var private_email = $("input#email_private").val();
var cdid = $("#cdid_hidden").val();
var cid=$('#cdid_hidden').val();
if($(this).is(email)) {
alert(1);
window.open('mailto:<?php echo $res['emailVal']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);
}
var url = "response.php?confirmEmail=confirm&cdid="+cdid;
$.get(url);
getDetails(cid);
});
});
答案 1 :(得分:0)
恕我直言,拥有两个dblclick处理程序会更容易。话虽如此,即使你正在监听两个输入的dblclick事件,你的代码中只检查电子邮件输入是否为空
if(email) {
alert(1);
window.open('mailto:<?php echo $res['email']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);
}
如果您不想拥有单独的事件处理程序,可以尝试检查类似
的ID$(document).ready(function(){
$("input#email, input#email_private").dblclick(function(){
var email = $("input#email").val();
var private_email = $("input#email_private").val();
var cdid = $("#cdid_hidden").val();
var cid=$('#cdid_hidden').val();
var clicked = $(this).attr('id');
if(clicked == 'email') {
alert(1);
window.open('mailto:<?php echo $res['email']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);
}
var url = "response.php?confirmEmail=confirm&cdid="+cdid;
$.get(url);
getDetails(cid);
});
});
答案 2 :(得分:0)
我想你想根据相关的输入双击显示警报1和2.我修改了你的代码来实现这个目标..
Jquery的:
$(document).ready(function(){
$("input#email, input#email_private").dblclick(function(){
var private_email = $("input#email_private").val();
var cdid = $("#cdid_hidden").val();
var cid=$('#cdid_hidden').val();
if($(this).attr('id') == 'email') {
alert(1);
window.open('mailto:<?php echo $res['email']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);
}
var url = "response.php?confirmEmail=confirm&cdid="+cdid;
$.get(url);
getDetails(cid);
});
});
答案 3 :(得分:0)
您可以通过获取元素的ID来检查触发事件的元素。然后在if
语句中,您可以检查元素ID并返回相应的操作。这是一个如何实现的片段
HTML CODE
<div>
<input type="text" placeholder="Email here" id="email" />
</div>
<div>
<input type="text" placeholder="Email Private here" id="email_private" />
</div>
JQUERY CODE
$(document).ready(function () {
$('input#email, input#email_private').dblclick(function (event) {
alert("Double clicked");
if (event.target.id === 'email') {
alert("Email double clicked");
// Email textbox triggered the event
// Do something
}
else if (event.target.id == 'email_private') {
alert("Email Private double clicked");
// Email private triggeredd this event, do something here
}
});
});