简体   繁体   English

jquery 如何使复选框始终处于选中状态

[英]jquery how to make checkbox always checked

How can I make a checkbox always be checked, with Jquery 1.4.2?如何使用 Jquery 1.4.2 始终选中复选框?

This is my html output from the struts application:这是我的 html output 来自 struts 应用程序:

<input type="checkbox" name="helloThere" value="on">

I tried:我试过了:

$(document).ready(function() {
    $('#helloThere').attr('checked', 'checked');
});



JSFIDDLE小提琴

http://jsfiddle.net/96p4zg9w/ this shows the current code, I would need the checkbox to load checked. http://jsfiddle.net/96p4zg9w/这显示了当前代码,我需要复选框加载选中。

The props property is not available in JQUERY 1.4.2 props 属性在 JQUERY 1.4.2 中不可用

your jquery selector is wrong. 您的jQuery选择器是错误的。

You are trying select an element with 'helloThere' id, but your input has not an id attribute. 您正在尝试选择具有'helloThere'ID的元素,但您的输入没有id属性。 You can add this attribute to your input field or change jquery selector. 您可以将此属性添加到输入字段或更改jquery选择器。

Try one of this tow solutions: 尝试以下两种解决方案之一:

<input type="checkbox" name="helloThere" id="helloThere" value="on">

or 要么

$(document).ready(function() {
    $("input[name='helloThere']").attr('checked', 'checked');
});

You need to put your JavaScript inside $(document).ready() . 您需要将JavaScript放入$(document).ready() This ensures all of the content you might want to touch has been loaded before you try to change it. 这样可以确保在尝试更改内容之前,您可能要触摸的所有内容均已加载。

In your scenario then, you would need: 那么在您的方案中,您将需要:

$(document).ready(function() {
    $('#helloThere').attr('checked', 'checked');
});

Edit : Your checkbox doesn't have an ID. 编辑 :您的复选框没有ID。 You need to add an ID of "helloThere" for $('#helloThere') to pick it up. 您需要为$('#helloThere')添加ID“ helloThere”, $('#helloThere')进行提取。 Your fiddle also needs to have jQuery loaded using the menu on the left. 您的小提琴还需要使用左侧菜单加载jQuery。 Here's a working version: http://jsfiddle.net/96p4zg9w/1/ 这是一个工作版本: http : //jsfiddle.net/96p4zg9w/1/

Check this jsfiddle for a working example of how you can do it using javascript. 查看此jsfiddle ,了解如何使用javascript进行工作的示例。 If you want to use versions of jQuery below 1.5, you need to do it this way: 如果要使用低于1.5的jQuery版本,则需要采用以下方式:

$("#checkbox").attr("checked", true);

or 要么

$("#checkbox").attr("checked", "checked");

If you want to select element using the name instead of id, use: 如果要使用名称而不是ID选择元素,请使用:

input[name='helloThere']

Hope it useful! 希望它有用!

It can be done in three ways. 它可以通过三种方式完成。 You can use checked in input that will always stay checked. 您可以使用签入输入,该输入将始终保持选中状态。

<input id="myId" type="checkbox" name="name" checked>

Using prop and assume input id is myId 使用prop并假设输入id为myId

 $(document).ready(function() {
     $(#myId).prop('checked', true);
 });

Before jQuery 1.6 using attr 在jQuery 1.6之前使用attr

 $(document).ready(function() {
     $(#ID).attr('checked','checked');
 });

 <lable>this will always be checked, even you try to uncheck</lable>: <input type="checkbox" checked onclick="return false;">

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

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