最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace checkbox with image button - Stack Overflow

programmeradmin0浏览0评论

I am looking for a way to allow for the user to select multiple months, days, hours and minutes where this will be used for scheduling tasks. The easiest way I can imagine doing this is using checkboxes for each element since they have a boolean properly I can use to determine if its selected or not.

In using this approach, I would like to either entire replace the checkbox with an image (different image for checked/unchecked) or just hide the existing image and style the text area to give the effect of an image using CSS.

How can I replace the default checkbox with an image or hide the default checkbox image entirely?

Example:

I am looking for a way to allow for the user to select multiple months, days, hours and minutes where this will be used for scheduling tasks. The easiest way I can imagine doing this is using checkboxes for each element since they have a boolean properly I can use to determine if its selected or not.

In using this approach, I would like to either entire replace the checkbox with an image (different image for checked/unchecked) or just hide the existing image and style the text area to give the effect of an image using CSS.

How can I replace the default checkbox with an image or hide the default checkbox image entirely?

Example:

Share Improve this question asked Apr 23, 2015 at 22:50 user470760user470760
Add a ment  | 

4 Answers 4

Reset to default 2

toggleClass() does this sort of thing:

http://jsfiddle/zsxyequj/

$('.btn').click(function() {
    $('.btn').toggleClass('col');
});

You can use a <label> for your checkbox and then hide it using CSS. Something like this should work:

HTML

<input type="checkbox" id="check" class="checkbox">
<label for="check"><img src="..."/></label>

CSS

input {
    display:none;
}

Anything you put inside the <label> tags will activate that checkbox when clicked.

Here is a fiddle of it in action. If you remove the display:none; you'll notice that clicking on the label contents checks the box.

http://jsfiddle/w61zb19y/

The advanced checkbox hack es to mind - this allows you to use a hidden checkbox to control an element to indicate on/off

In this example below, clicking the image changes the styles by which you can indicate selected or not

Here's one way to use it

HTML

<table id="calendar">
<tbody>
<tr>
<td>
<!-- duplicate this table cell as many times as needed but
 give each input an id and update the label's for attributes -->
<label for="toggle-1"><img src="someimage.jpg"><!-- this can be an image, text whatever --></label>
<input type="checkbox" id="toggle-1">
<div>select</div>
</td>
</tr>
</tbody>
</table>

CSS

/* Checkbox Hack */

#calendar input[type=checkbox] {
   position: absolute;
   top: -9999px;
   left: -9999px;
}
#calendar label { 
  -webkit-appearance: push-button;
  -moz-appearance: button; 
  display: inline-block;
  margin: 60px 0 10px 0;
  cursor: pointer;
}

/* Default State */
#calendar div {
   background: red;
   width: 400px;
   height: 100px;
   line-height: 100px;
   color: white;
   text-align: center;
}

/* Toggled State */
#calendar input[type=checkbox]:checked ~ div {
   background: green;
}

#calendar input[type=checkbox]:checked ~ div:after {
    content: "ed"
}

And here's an example I put together after playing a little more where it doesn't look like a separate elements:

http://jsfiddle/8nz1k2wb/

It's something you can change to suit your needs

http://timpietrusky./advanced-checkbox-hack

you can't design the checkbox itself but you can hide it and use other element for the design, the design of the element will be changed by the checkbox situation. replace the design to whatever you like...

HTML:

<label class="checkbox">
    <input type="checkbox" />
    <span>option</span>
</label>

CSS:

.checkbox input[type="checkbox"]
{
    display: none; 
}

.checkbox span
{
    display: inline-block;
    width: 18px;
    text-indent: 25px;
    border: 1px solid;
}

.checkbox input[type="checkbox"]:checked + span
{
    background: #ccc;    
}

fiddle: http://jsfiddle/62ptgwee/1/

发布评论

评论列表(0)

  1. 暂无评论