在HTML上进行默认检查的单选按钮值显示?

发布于 2025-02-07 12:34:59 字数 1490 浏览 0 评论 0 原文

因此,我的JS函数可以检查是否选择了单选按钮,然后像正常情况一样在我的HTML上显示值。

我的问题是它仅在设置为0时才有效。我将如何更改此操作,以便如果我在我的HTML无线电输入中使用$ 5的检查选项,它也会在我的浏览器中显示该值吗?

如果我将检查选项更改为其他提示值,它仍然显示为0作为起始值,因为我没有单击任何内容。

这是我的JS代码

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

document.addEventListener("click", ({
      target
    }) => {
      if (target.className === "tips" && target.checked) {
        window.tip = parseInt(target.value);
      } else {
        return;

        tipTotal.textContent = `$${window.tip}`;
      }

我显示选定的广播按钮,

<td id="tip">$0.00</td>

这是我的HTML广播按钮

<input type="radio" value="0" id="tip0" name="tip" class="tips" />
<label for="tip0">$0</label>
</div>
<div>
  <input type="radio" value="300" id="tip1" name="tip" class="tips" />
  <label for="tip1">$3</label>
</div>
<div>
  <input type="radio" value="500" id="tip2" name="tip" class="tips" checked //here is the default checked option for $5 />
  <label for="tip2">$5</label>
</div>  

,因此,默认情况下,我的应用将显示提示:$ 5 选项已选中,但是,浏览器上的HTML for 提示会说提示:$ 0.00 而不是提示:$ 5.00 ,除非我单击它

So I have my JS Function that checks if a radio button is selected, then displays the value on my HTML like normal.

My problem is it only works if the default value is set to 0. How would I change this so that if I use the checked option on $5 in my HTML radio input, that it will also display that value in my browser?

If I change my checked option to a different tip value, it still shows 0 as the starting value since I didn't click anything.

Here is my JS Code

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

document.addEventListener("click", ({
      target
    }) => {
      if (target.className === "tips" && target.checked) {
        window.tip = parseInt(target.value);
      } else {
        return;

        tipTotal.textContent = `${window.tip}`;
      }

I display the selected radio button here

<td id="tip">$0.00</td>

Here is my HTML Radio Buttons

<input type="radio" value="0" id="tip0" name="tip" class="tips" />
<label for="tip0">$0</label>
</div>
<div>
  <input type="radio" value="300" id="tip1" name="tip" class="tips" />
  <label for="tip1">$3</label>
</div>
<div>
  <input type="radio" value="500" id="tip2" name="tip" class="tips" checked //here is the default checked option for $5 />
  <label for="tip2">$5</label>
</div>  

So by default, my app will show the tip: $5 option checked, however, the HTML on the browser for Tips will says Tip: $0.00 instead of Tip: $5.00 unless I click it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只等公子 2025-02-14 12:34:59

它仅在单击单击单击单击时,才设置 window.tip ,因为您添加了仅在单击时触发的事件侦听器。您可以添加一个on Load Event侦听器。

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

document.addEventListener("click", ({ target }) => {
    if (target.className === "tips" && target.checked) {
      window.tip = parseInt(target.value);
    }
});

document.addEventListener("load", () => {
    const selectedOption = document.querySelector(".tips:checked");
    window.tip = parseInt(selectedOption.value);
});

我还在第一个事件听众中删除了不必要的其他陈述。

编辑

自动更新 tiptotal 文本:

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

function updateTip(value) {
    window.tip = parseInt(value);
    tipTotal.textContent = window.tip;
}

document.addEventListener("click", ({ target }) => {
    if (target.className === "tips" && target.checked) {
      updateTip(target.value);
    }
});

document.addEventListener("load", () => {
    const selectedOption = document.querySelector(".tips:checked");
    updateTip(selectedOption.value);
});

It sets window.tip only when a radio button is clicked because you added an event listener that fires only on click. You can add an on load event listener.

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

document.addEventListener("click", ({ target }) => {
    if (target.className === "tips" && target.checked) {
      window.tip = parseInt(target.value);
    }
});

document.addEventListener("load", () => {
    const selectedOption = document.querySelector(".tips:checked");
    window.tip = parseInt(selectedOption.value);
});

I also removed an unnecessary else statement in the first event listener.

EDIT

Automatically update tipTotal text:

window.tip = 0;

const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

function updateTip(value) {
    window.tip = parseInt(value);
    tipTotal.textContent = window.tip;
}

document.addEventListener("click", ({ target }) => {
    if (target.className === "tips" && target.checked) {
      updateTip(target.value);
    }
});

document.addEventListener("load", () => {
    const selectedOption = document.querySelector(".tips:checked");
    updateTip(selectedOption.value);
});
酒废 2025-02-14 12:34:59

首先,您还拥有一个名为“提示”的全局变量,还有一个带有 ID 的元素。具有 id s的元素也成为全局,因此您在命名方面有冲突。

接下来,您需要在JS中以及提示量更改时最初更新该 TD

let tip = 0;

const tipTotal = document.getElementById("tipAmount");
const orderTotal = document.getElementById("total");

// Initialize the table data
tipTotal.textContent = document.querySelector(".tips:checked").value;

document.addEventListener("click", function(event) {
  // No need to see if it's checked because clicking
  // on a radio button makes it checked and this code
  // wouldn't be called if you didn't click it.
  if (event.target.className === "tips") {
    tipTotal.textContent = event.target.value  // Update the table
  }
});
<div>
  <input type="radio" value="0" 
         id="tip0" name="tip" class="tips">
  <label for="tip0">$0</label>
</div>
<div>
  <input type="radio" value="300"
         id="tip1" name="tip" class="tips">
  <label for="tip1">$3</label>
</div>
<div>
  <input type="radio" value="500" 
         id="tip2" name="tip" class="tips" checked>
  <label for="tip2">$5</label>
</div>

<table>
  <tr><td id="tipAmount"></td></tr>
</table>

First, you've got a Global variable called "tip" and an element with an id of "tip" as well. Elements with ids also become Global and so you've got a conflict in naming.

Next, you need to initially update that td in your JS and also when the tip amount changes.

let tip = 0;

const tipTotal = document.getElementById("tipAmount");
const orderTotal = document.getElementById("total");

// Initialize the table data
tipTotal.textContent = document.querySelector(".tips:checked").value;

document.addEventListener("click", function(event) {
  // No need to see if it's checked because clicking
  // on a radio button makes it checked and this code
  // wouldn't be called if you didn't click it.
  if (event.target.className === "tips") {
    tipTotal.textContent = event.target.value  // Update the table
  }
});
<div>
  <input type="radio" value="0" 
         id="tip0" name="tip" class="tips">
  <label for="tip0">$0</label>
</div>
<div>
  <input type="radio" value="300"
         id="tip1" name="tip" class="tips">
  <label for="tip1">$3</label>
</div>
<div>
  <input type="radio" value="500" 
         id="tip2" name="tip" class="tips" checked>
  <label for="tip2">$5</label>
</div>

<table>
  <tr><td id="tipAmount"></td></tr>
</table>

放低过去 2025-02-14 12:34:59

只需触发 tip2 单击“单击事件”事件:

document.getElementById("tip2").click();

例如:

const tipTotal = document.getElementById("tip");
const tipButton = document.getElementById("tip2");

tipButton.addEventListener("click", ({ target }) => {
  if (target.className === "tips" && target.checked) {
    window.tip.innerText = "$" + target.value;
  } else {
    return;
  }
});

// activate click event on tip2 radio button
tipButton.click();
<div id="tip">$0.00</div>

<div>
  <input type="radio" value="5.00" id="tip2" name="tip" class="tips" />
  <label for="tip2">$5</label>
</div>

Simply trigger a tip2 radio button click event:

document.getElementById("tip2").click();

For example:

const tipTotal = document.getElementById("tip");
const tipButton = document.getElementById("tip2");

tipButton.addEventListener("click", ({ target }) => {
  if (target.className === "tips" && target.checked) {
    window.tip.innerText = "$" + target.value;
  } else {
    return;
  }
});

// activate click event on tip2 radio button
tipButton.click();
<div id="tip">$0.00</div>

<div>
  <input type="radio" value="5.00" id="tip2" name="tip" class="tips" />
  <label for="tip2">$5</label>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文