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

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

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

问题

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

  1. async function search() {
  2. const email = document.getElementById("email").value.trim();
  3. if (!email) return alert("错误。");
  4. const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
  5. const token = "我的API密钥";
  6. const method = "GET";
  7. try {
  8. const res = await fetch(url, {
  9. method,
  10. headers: {
  11. "Authorization": `Bearer ${token}`
  12. }
  13. });
  14. if (!res.ok) throw new Error("结果不正常");
  15. const data = await res.json();
  16. // 如果API没有响应,可以使用以下数据生成结果
  17. var jsondata = [{
  18. "CARGO": "JEFE",
  19. "CIUDAD": "CIUDAD1",
  20. "EMAIL": "jperez",
  21. "Función": "OPERACIONES",
  22. "NOMBRE": "JUAN PEREZ",
  23. "Unidad organizativa": "OPERACIONES"
  24. }]
  25. let text = ""
  26. var output = document.getElementById("output");
  27. output.innerHTML = "搜索中...";
  28. var keys = [];
  29. text += "<table border==\"1\"><tr>";
  30. for (key in data[0]) {
  31. text += '<td>' + key + '</td>';
  32. }
  33. text += "</tr>";
  34. for (var i = 0; i < data.length; i++) {
  35. text += '<tr>';
  36. for (key in data[i]) {
  37. text += '<td>' + data[i][key] + '</td>';
  38. }
  39. text += '</tr>';
  40. }
  41. text += "</table>";
  42. output.innerHTML = text;
  43. } catch (err) {
  44. alert(err);
  45. }
  46. }
  1. <form onsubmit="search(); event.preventDefault(); return false">
  2. <label>EMAIL: <input type=search id="email"></label>
  3. <button type=submit>搜索</button>
  4. </form>
  5. <hr />
  6. <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 -->

  1. async function search() {
  2. const email = document.getElementById(&quot;email&quot;).value.trim();
  3. if (!email) return alert(&quot;error.&quot;);
  4. const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
  5. const token = &quot;my-api-key&quot;;
  6. const method = &quot;GET&quot;;
  7. try {
  8. const res = await fetch(url, {
  9. method,
  10. headers: {
  11. &quot;Authorization&quot;: `Bearer ${token}`
  12. }
  13. });
  14. if (!res.ok) throw new Error(&quot;Result not OK&quot;);
  15. const data = await res.json();
  16. // if api didnt respond use this instead data to generate result
  17. var jsondata = [{
  18. &quot;CARGO&quot;: &quot;JEFE&quot;,
  19. &quot;CIUDAD&quot;: &quot;CIUDAD1&quot;,
  20. &quot;EMAIL&quot;: &quot;jperez&quot;,
  21. &quot;Funci&#243;n&quot;: &quot;OPERACIONES&quot;,
  22. &quot;NOMBRE&quot;: &quot;JUAN PEREZ&quot;,
  23. &quot;Unidad organizativa&quot;: &quot;OPERACIONES&quot;
  24. }]
  25. let text = &quot;&quot;
  26. var output = document.getElementById(&quot;output&quot;);
  27. output.innerHTML = &quot;Searching...&quot;;
  28. var keys = [];
  29. text += &quot;&lt;table border==\&quot;1\&quot;&gt;&lt;tr&gt;&quot;;
  30. for (key in data[0]) {
  31. text += &#39;&lt;td&gt;&#39; + key + &#39;&lt;/td&gt;&#39;;
  32. }
  33. text += &quot;&lt;/tr&gt;&quot;;
  34. for (var i = 0; i &lt; data.length; i++) {
  35. text += &#39;&lt;tr&gt;&#39;;
  36. for (key in data[i]) {
  37. text += &#39;&lt;td&gt;&#39; + data[i][key] + &#39;&lt;/td&gt;&#39;;
  38. }
  39. text += &#39;&lt;/tr&gt;&#39;;
  40. }
  41. text += &quot;&lt;/table&gt;&quot;;
  42. output.innerHTML = text;
  43. } catch (err) {
  44. alert(err);
  45. }
  46. }

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

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

<!-- end snippet -->

答案1

得分: 2

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

  1. let emailFound = false

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

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

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

英文:

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

  1. 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:

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

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:

确定