gpt4 book ai didi

javascript - 选中/未选中的复选框

转载 作者:行者123 更新时间:2023-11-28 21:16:17 27 4
gpt4 key购买 nike

我试图在复选框处于选中状态或未选中状态时传递一个值。

但是,它似乎没有通过非检查状态。我正在使用的代码如下:

if (document.getElementById('PRODUCT_REVIEW_EMAILS_FIELD').checked == true){
document.getElementById('PRODUCT_REVIEW_EMAILS_FIELD').value = 'on';
}
else {
document.getElementById('PRODUCT_REVIEW_EMAILS_FIELD').value = 'off';
}

我添加了一个警报:

alert(document.getElementById('PRODUCT_REVIEW_EMAILS_FIELD').value);

令人惊讶的是,它显示了“off”值 - 然而 - 这并没有成功通过。

我错过了什么?

最佳答案

这是正常的、预期的且明确的行为。

  • 复选框具有任意
  • 当复选框的 checked 属性处于启用状态时,它将作为具有该值的表单的一部分提交;
  • 当复选框的 checked 属性关闭时,根本不会提交。
<小时/>

HTML 4.01 says :

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

And :

When the user submits a form (e.g., by activating a submit button), the user agent processes it as follows.

  • Step one: Identify the successful controls
  • Step two: Build a form data set A form data set is a sequence of control-name/current-value pairs constructed from successful controls. [..]
<小时/>

HTML5 says similar things .

<小时/>

您可以将后端代码写入期望具有特定名称的字段,并在缺少这些字段时做出相应的 react 。

关于javascript - 选中/未选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7505692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com