Html 已检查 =“已检查”在 chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16950751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:08:44  来源:igfitidea点击:

checked="checked" not working in chrome

htmlcheckbox

提问by chuckieDub

<input type="checkbox" name="Type[]" value="Red" checked="checked" /><span class="space-right">Red</span>

Properly sets checkbox to checked in firefox and safari but not chrome. Can't find any info about this online.

将复选框正确设置为在 Firefox 和 safari 中签入,但在 chrome 中不签入。无法在网上找到有关此的任何信息。

Anyone know how to fix this?

有人知道怎么修这个东西吗?

Have also tried the naked checkedas well as checked="true"

也试过裸体checked以及checked="true"

Not looking for a js solution, thank you.

不是在寻找js解决方案,谢谢。

Edit: The answer by taco below describes what the issue was. When using forms and input elements, the elements must be properly nested in <td></td>tags or the checked="checked"has no effect. Here is an example of a jsfiddle that proves this to be true on chrome 29.0.1547.57: http://jsfiddle.net/LnL7b/

编辑:下面 taco 的回答描述了问题所在。使用表单和输入元素时,元素必须正确嵌套在<td></td>标签中,否则checked="checked"无效。下面是一个 jsfiddle 的例子,它在 chrome 29.0.1547.57 上证明这是真的:http: //jsfiddle.net/LnL7b/

回答by taco

I was able to replicate this issue on Google Chrome Version 28.0.1500.95.

我能够在 Google Chrome 版本 28.0.1500.95 上复制这个问题。

<table>
  <tr>
    <td>test</td>
    <input type="radio" name="foo" value="bar" checked="checked">
  </tr>
</table>

I broke the table by improperly nesting a radio button. This somehow causes Google Chrome to not mark the input radio as checked.

我通过不正确地嵌套一个单选按钮打破了表格。这以某种方式导致谷歌浏览器没有将输入收音机标记为选中。

jsfiddle example - brokenand jsfiddle example - working

jsfiddle 示例 - 损坏jsfiddle 示例 - 工作

回答by NguyenHuy

Try with tag DIV

尝试使用标签 DIV

<div><input type="checkbox" name="ckkhuyenmai" id="ckkhuyenmai" checked>KHUYENMAI</div>

It will be fine.

没事的。

回答by 2D3D4D

checked="checked" works fine in Chrome. Make sure you are not having any other issues. Any invalid HTML. No inline element containing block-level elements?

已检查 =“已检查”在 Chrome 中工作正常。确保您没有任何其他问题。任何无效的 HTML。没有包含块级元素的内联元素?

回答by Umesh Patil

@Abhay's answer worked for me, I dont know why people negative marking it. I had 2 radio groups sharing same name like below,

@Abhay 的回答对我有用,我不知道为什么人们会对它进行负面标记。我有 2 个同名的广播组,如下所示,

  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other 

Then again at the bottom of the page,

然后再次在页面底部,

  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other 

So what happening was browser's getting ambiguity and it was selecting only the bottom one not the upper one.

所以发生的事情是浏览器变得模糊不清,它只选择底部而不是顶部。

Hope this will help someone.

希望这会帮助某人。

Enjoy ;)

享受 ;)

回答by ?ào Thi?n Tam

You provided a 'checked'prop to a form field without an 'onChange' handler. This will render a read-only field. If the field should be mutable use 'defaultChecked'. Otherwise, set either 'onChange' or 'readOnly'.

您为没有“onChange”处理程序的表单字段提供了“已检查”道具。这将呈现只读字段。如果该字段应该是可变的,请使用'defaultChecked'。否则,设置“onChange”或“readOnly”。

回答by Supplement

i know you said you weren't looking to use it but jquery is an option:

我知道你说过你不想使用它,但 jquery 是一个选择:

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>

<form>
  <p>
    <input type="checkbox" name="newsletter" value="Hourly" checked="checked">

    <input type="checkbox" name="newsletter" value="Daily">
    <input type="checkbox" name="newsletter" value="Weekly">

    <input type="checkbox" name="newsletter" value="Monthly" checked>
    <input type="checkbox" name="newsletter" value="Yearly">
  </p>
</form>
<div></div>

<script>
var countChecked = function() {
  var n = $( "input:checked" ).length;
  $( "div" ).text( n + (n === 1 ? " is" : " are") + " checked!" );
};
countChecked();

$( "input[type=checkbox]" ).on( "click", countChecked );
</script>

</body>
</html>

http://jsfiddle.net/HnEgT/

http://jsfiddle.net/HnEgT/

Hope this helps some what.

希望这有助于一些什么。

回答by Abhay

Checked="checked" works fine in Chrome. Ensure that there are not multiples of same name. Also check for hidden ones.

Checked="checked" 在 Chrome 中工作正常。确保没有多个同名。还要检查隐藏的。

回答by MVC

If you have html like this:

如果你有这样的 html:

<input type="checkbox" name="email">
<input type="checkbox" name="email">
<input type="checkbox" name="email" checked>

Then no one will be checked, due to name conflicts.

那么由于名称冲突,将不会检查任何人。