简体   繁体   English

jQuery检查要检查的标签之前的上一个复选框

[英]jquery check previous check-box before a tag to be checked

I have a code like 我有一个类似的代码

<input type="checkbox" name="abc" value="A" class="chbx">
<a href="#" class="checkbox-selector">href1</a>
<input type="checkbox" name="abc" value="b" class="chbx">
<a href="#" class="checkbox-selector">href2</a>
<input type="checkbox" name="abc" value="c" class="chbx">
<a href="#" class="checkbox-selector">href3</a>

What I desired is when I click on href1 checkbox 1st should be selected and when I click on href2 , the 2nd checkbox should be selected. 我想要的是,当我单击href1复选框1st时应选中,而当我单击href2 ,应选中第二个复选框。

How can this be done in jQuery ? 如何在jQuery中完成呢?

You could go about this in a different way and not use javascript at all 您可以通过其他方式解决此问题,而根本不使用javascript

Try this: 尝试这个:

<label><input type="checkbox" value="hello" name="chkbox1" /> Here is the text</label>

<label><input type="checkbox" value="hello" name="chkbox2" /> Here is the text</label>

<label><input type="checkbox" value="hello" name="chkbox3" /> Here is the text</label>

You don't need jQuery. 您不需要jQuery。 You can use plain old html... 您可以使用普通的旧html ...

<input type="checkbox" name="abc" id="chk1" value="A" class="chbx">
<label for="chk1" class="checkbox-selector">href1</label>
<input type="checkbox" name="abc" id="chk2" value="b" class="chbx">
<label for="chk2" class="checkbox-selector">href2</label>
<input type="checkbox" name="abc" id="chk3" value="c" class="chbx">
<label for="chk3" class="checkbox-selector">href3</label>

To add support to older browsers (eg ie6 suggested by Fabricio) use the jQuery approach suggested by scessor: 要增加对旧版浏览器的支持(例如Fabricio建议的ie6),请使用scessor建议的jQuery方法:

$('.checkbox-selector').click(function() {
    $(this).prev().prop('checked', true);
});

Note that you should implement both and only enable the javscript approach if you're using a browser that doesn't support labels. 请注意,您应该同时实现这两种方法,并且仅在使用不支持标签的浏览器时才启用javscript方法。

If you use a label, it will automatically check the checkbox for you as long as you give the checkbox an ID and set the FOR attribute in the label. 如果您使用标签,则只要您为复选框提供一个ID并在标签中设置FOR属性,它将自动为您选中该复选框。

<input type="checkbox" name="abc" value="A" class="chbx" id="c1">
<label for="c1" class="checkbox-selector">href1</label>
<input type="checkbox" name="abc" value="b" class="chbx" id="c2">
<label for="c2" class="checkbox-selector">href2</label>
<input type="checkbox" name="abc" value="c" class="chbx" id="c3">
<label for="c3" class="checkbox-selector">href3</label>

Any styles you applied to the former hyperlink should still apply to the label. 您应用于前超链接的任何样式仍应应用于标签。

Fully working example which checks and unchecks the boxes on click: 完整的工作示例,它选中并取消选中单击框:

$('.checkbox-selector').click(function() {
    var chb = $(this).prev(); //caches selector
    chb.prop('checked', !chb.prop('checked')); //inverses checked state
});

Fiddle 小提琴

But honestly, unless you really need compatibility with older browsers such as ie6 and other non-html5 browsers, you should use the <label> s method. 但老实说,除非您确实确实需要与旧的浏览器(例如ie6和其他非html5浏览器)兼容,否则应使用<label> s方法。


If by name you meant the link's description, just use $(this).text() to get the clicked link's text. 如果按name表示链接的描述,则只需使用$(this).text()即可获得单击的链接的文本。
Fiddle 小提琴

You have to do this: 您必须这样做:

$('.checkbox-selector').click(function(){
    $(this).prev().attr('checked', true);
});

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

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