显示警报,如果在输入搜索中未找到值(”email”)。

huangapple go评论77阅读模式
英文:

Show an alert if the value ("email") was not found in the input search

问题

你好,以下是您的代码的中文翻译:

async function search() {
  const email = document.getElementById("email").value.trim();
  if (!email) return alert("错误。");
  const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
  const token = "我的API密钥";
  const method = "GET";
  try {
    const res = await fetch(url, {
      method,
      headers: {
        "Authorization": `Bearer ${token}`
      }
    });

    if (!res.ok) throw new Error("结果不正常");
    const data = await res.json();

    // 如果API没有响应,可以使用以下数据生成结果
    var jsondata = [{
      "CARGO": "JEFE",
      "CIUDAD": "CIUDAD1",
      "EMAIL": "jperez",
      "Función": "OPERACIONES",
      "NOMBRE": "JUAN PEREZ",
      "Unidad organizativa": "OPERACIONES"
    }]
    let text = ""
    var output = document.getElementById("output");
    output.innerHTML = "搜索中...";
    var keys = [];
    text += "<table border==\"1\"><tr>";
    for (key in data[0]) {
      text += '<td>' + key + '</td>';
    }
    text += "</tr>";
    for (var i = 0; i < data.length; i++) {
      text += '<tr>';
      for (key in data[i]) {
        text += '<td>' + data[i][key] + '</td>';
      }
      text += '</tr>';
    }
    text += "</table>";

    output.innerHTML = text;
  } catch (err) {
    alert(err);
  }
}
<form onsubmit="search(); event.preventDefault(); return false">
  <label>EMAIL: <input type=search id="email"></label>
  <button type=submit>搜索</button>
</form>
<hr />
<div id=output></div>

希望这对您有所帮助!如果您有其他问题,请随时提问。

英文:

I have a function that looks for an email and displays the data from an api call. I want to display an alert('Email not found') if the email does not exist in the data. How can I achieve this?

This is my code that I have

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

async function search() {
const email = document.getElementById(&quot;email&quot;).value.trim();
if (!email) return alert(&quot;error.&quot;);
const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
const token = &quot;my-api-key&quot;;
const method = &quot;GET&quot;;
try {
const res = await fetch(url, {
method,
headers: {
&quot;Authorization&quot;: `Bearer ${token}`
}
});
if (!res.ok) throw new Error(&quot;Result not OK&quot;);
const data = await res.json();
// if api didnt respond use this instead data to generate result
var jsondata = [{
&quot;CARGO&quot;: &quot;JEFE&quot;,
&quot;CIUDAD&quot;: &quot;CIUDAD1&quot;,
&quot;EMAIL&quot;: &quot;jperez&quot;,
&quot;Funci&#243;n&quot;: &quot;OPERACIONES&quot;,
&quot;NOMBRE&quot;: &quot;JUAN PEREZ&quot;,
&quot;Unidad organizativa&quot;: &quot;OPERACIONES&quot;
}]
let text = &quot;&quot;
var output = document.getElementById(&quot;output&quot;);
output.innerHTML = &quot;Searching...&quot;;
var keys = [];
text += &quot;&lt;table border==\&quot;1\&quot;&gt;&lt;tr&gt;&quot;;
for (key in data[0]) {
text += &#39;&lt;td&gt;&#39; + key + &#39;&lt;/td&gt;&#39;;
}
text += &quot;&lt;/tr&gt;&quot;;
for (var i = 0; i &lt; data.length; i++) {
text += &#39;&lt;tr&gt;&#39;;
for (key in data[i]) {
text += &#39;&lt;td&gt;&#39; + data[i][key] + &#39;&lt;/td&gt;&#39;;
}
text += &#39;&lt;/tr&gt;&#39;;
}
text += &quot;&lt;/table&gt;&quot;;
output.innerHTML = text;
} catch (err) {
alert(err);
}
}

<!-- language: lang-html -->

&lt;form onsubmit=&quot;search(); event.preventDefault(); return false&quot;&gt;
&lt;label&gt;EMAIL: &lt;input type=search id=&quot;email&quot;&gt;&lt;/label&gt;
&lt;button type=submit&gt;Search&lt;/button&gt;
&lt;/form&gt;
&lt;hr /&gt;
&lt;div id=output&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

你应该在函数开始处设置一个标志变量,并将其初始化为false,例如:

let emailFound = false

然后,在结果循环内,你可以将每个电子邮件与初始电子邮件进行比较,如果找到匹配项,将标志设置为true,例如:

if (data[i].email == email) {
  emailFound = true
}

如果在循环结束时,标志仍然为false,这意味着未找到电子邮件,因此你可以安全地显示适当的警告消息。

英文:

You should set a flag variable at the beginning of your function and initialize it to false e.g.:

let emailFound = false

Then, inside the results loop you can compare each email with the initial one and if a match is found, set the flag to true like:

if (data[i].email == email) {
emailFound = true
} 

If at the end of the loop, the flag is still false that means that the email was not found so you can safely show the suitable alert message.

huangapple
  • 本文由 发表于 2023年4月20日 05:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定