简体   繁体   English

否则发送按钮事件监听器

[英]send button EventListener if else

I have a feedback form with 3 clickable images for user satisfaction rating: [1]: https://i.stack.imgur.com/hsUkd.png我有一个包含 3 个可点击图像的用户满意度评分反馈表:[1]: https://i.stack.imgur.com/hsUkd.png

Fairly new to javascript. javascript 相当新。 When I run the.addeventListener on my script it works on the first if statement, and if I remove elses it alternates on which if runs.当我在我的脚本上运行 .addeventListener 时,它适用于第一个 if 语句,如果我删除 else,它会交替运行 if 语句。 below is the html and javascript:下面是 html 和 javascript:

 <div id="panel" class="panel-container">
      <strong>How satisfied are you with our customer support performance?</strong>
      <div class="ratings-container">
        <div class="rating" id="sad">
          <img src="https://image.flaticon.com/icons/svg/187/187150.svg" alt=""/>
          <small>Unhappy</small>
        </div>
        <div class="rating" id="meh">
          <img src="https://image.flaticon.com/icons/svg/187/187136.svg" alt=""/>
          <small>Neutral</small>
        </div>
        <div class="rating active" id="happy">
          <img src="https://image.flaticon.com/icons/svg/187/187133.svg" alt=""/>
          <small>Satisfied</small>
        </div>
      </div>
      <button class="btn" id="send">Send review</button>
    </div>

and the Javascript:和 Javascript:

const ratingsEl = document.querySelectorAll(".rating");
const sendBtn = document.querySelector("#send");
const panel = document.querySelector("#panel");
const sad = document.querySelector("#sad");
const meh = document.querySelector("#meh");
const happy = document.querySelector("#happy");



ratingsEl.forEach((el) => {
  el.addEventListener("click", () => {
    ratingsEl.forEach((innerEl) => {
      innerEl.classList.remove("active");
    });

    el.classList.add("active");
  });
});

sendBtn.addEventListener("click", () => {
    if(sad){
        panel.innerHTML = `
        <p>sad</p>
    `;
    }

     else if(meh){
        panel.innerHTML = `
        <p>meh</p>
    `;
    }
     else if(happy){
        panel.innerHTML = `
        <p>feedack for this</p>
    `;
    }
});

I have tried using the:我试过使用:

ev.currentTarget.id == "myId" 

function in my if blocks and tried a different event listener for each block, but did not work. function 在我的 if 块中,并为每个块尝试了不同的事件侦听器,但没有奏效。

i thought of using the我想用

button.tagName === "myId"

statement at some point in my if, but i am using one button for all 3.在我的 if 中的某个时间点声明,但我对所有 3 个按钮都使用一个按钮。

Please help请帮忙

Keep track of the active review in a global variable and update it when a new review is selected and update that when the button is clicked.跟踪全局变量中的活动评论,并在选择新评论时更新它,并在单击按钮时更新它。

Doing so you don't have to individually select all the elements and check which one has the active class.这样做您不必单独检查所有元素 select 并检查哪个元素具有active的 class。

 const ratingsEl = document.querySelectorAll(".rating"); const sendBtn = document.querySelector("#send"); const panel = document.querySelector("#panel"); let review = "happy"; ratingsEl.forEach((el) => { el.addEventListener("click", (e) => { ratingsEl.forEach((innerEl) => { innerEl.classList.remove("active"); }); el.classList.add("active"); review = el.id; }); }); sendBtn.addEventListener("click", () => { panel.innerHTML = `<p>${review}</p>`; });
 img { width: 20px; }.rating { display: flex; align-items: center; margin: 0.5rem 0; }.rating small { margin-left: 0.5rem; cursor: pointer; }.rating.active small{ font-weight: bold; }
 <div id="panel" class="panel-container"> <strong>How satisfied are you with our customer support performance?</strong> <div class="ratings-container"> <div class="rating" id="sad"> <img src="https://image.flaticon.com/icons/svg/187/187150.svg" alt="" /> <small>Unhappy</small> </div> <div class="rating" id="meh"> <img src="https://image.flaticon.com/icons/svg/187/187136.svg" alt="" /> <small>Neutral</small> </div> <div class="rating active" id="happy"> <img src="https://image.flaticon.com/icons/svg/187/187133.svg" alt="" /> <small>Satisfied</small> </div> </div> <button class="btn" id="send">Send review</button> </div>

Or, you can individually check which element has the active class in it.或者,您可以单独检查哪个元素中包含active的 class。

Also, I would suggest avoid setting content using innerHTML , instead create an element and append it.另外,我建议避免使用innerHTML设置内容,而是创建一个元素并 append 它。

sendBtn.addEventListener("click", () => {
  panel.innerHTML = "";
  const usrReview = document.createElement("p");
  usrReview.innerText = review;
  panel.appendChild(usrReview);
});

Consider the code you have written:考虑您编写的代码:

if (sad) {
    // ...
}
else if (meh) {
    // ...
}
else if (happy) {
    // ...
}

sad , meh and happy all evalutes to true when used as a condition.当用作条件时, sadmehhappy都评估为true Since the condition of the first if evalutes to true , only that block runs everytime.由于第一个 if 的条件评估为true ,因此每次只有该块运行。

You should check which element among the sad , meh and happy has the active class.您应该检查sadmehhappy中的哪个元素具有active的 class。

Please use the following code instead请改用以下代码

sendBtn.addEventListener("click", () => {
    if(sad.classList.contains("active")){
        panel.innerHTML = `
        <p>sad</p>
    `;
    }

    else if(meh.classList.contains("active")){
        panel.innerHTML = `
        <p>meh</p>
    `;
    }

    else if(happy.classList.contains("active")){
        panel.innerHTML = `
        <p>feedack for this</p>
    `;
    }
});

Adding to the answers above.添加到上面的答案。 You can also use the toggle if all you want to do is add and remove it.如果您只想添加和删除它,您也可以使用切换。

https://jsfiddle.net/Lmqdhgnj/3/ https://jsfiddle.net/Lmqdhgnj/3/

const ratingsEl = document.querySelectorAll(".rating");
const sendBtn = document.querySelector("#send");
const panel = document.querySelector("#panel");
const sad = document.querySelector("#sad");
const meh = document.querySelector("#meh");
const happy = document.querySelector("#happy");

ratingsEl.forEach((el) => {
  el.addEventListener("click", (event) => {
    toggle(el,"active");
  });
});

function toggle(el, className) {
        el.classList.toggle(className);
}

sendBtn.addEventListener("click", () => {
        console.log(sad.className, meh.className, happy.className)
    if(sad.className.includes("active")){
        panel.innerHTML = `
        <p>sad</p>
    `;
    }

     if(meh.className.includes("active")){
        panel.innerHTML = `
        <p>meh</p>
    `;
    }
    if(happy.className.includes("active")){
        panel.innerHTML = `
        <p>feedack for this</p>
    `;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM