两个输入元素在焦点事件上返回相同的ID

问题描述 投票:1回答:1

我有两个具有不同ID的输入。当焦点事件处理程序触发时,两个输入似乎具有相同的ID。其他事件(例如模糊)不会出现此问题。这是我剥离的代码来说明问题。 event.target.id返回相同的值。这是怎么回事?

$('#first, #second').focus(function() {
  id = this.id;
  console.log(id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="first" type="text"><br/>
<input id="second" type="text">

编辑

此最小示例显示了我的问题

HTML
<!doctype html>
<html lang="en-gb"><head>
</head>
<body>
<?php include "../php/main/printopp.php"; ?>
<script src="../js/jquery-2.2.4.min.js"></script>
<script src="../js/printopp.min.js"></script>
</body>
</html>

PHP
<?php
echo '<input id="first" type="text">';
echo '<input id="second" type="text">';

JQUERY
$('#first, #second').focus(function(event) {
  id = this.id;
  console.log(id);
});

但是自那时以来,我发现问题并不完全像我所说的那样。如果单击first,然后单击second,则两次单击都将返回“ first”。如果单击second,然后单击first,则两者都返回“ second”。以先获胜者为准。

jquery
1个回答
0
投票

我已经完全重建了应用程序,一点一点地粘贴了先前脚本中的文本,并在进行过程中进行了测试。故障没有再次出现。据推测,这是由于某些杂散的隐藏字符或类似的故障引起的。

© www.soinside.com 2019 - 2024. All rights reserved.