jquery - 单击链接时如何选择和取消选择复选框

标签 jquery html css

我有以下复选框,当它被点击时,它会传递一个名称,如下所示

JSFiddle

复选框工作正常,我想要实现的是,当用户点击 NAME 时,复选框应该被选中,如果他再次点击它,它应该被取消选中。 我不确定该怎么做,目前如上所示,您可以单击复选框,当您取消选择它时它会变为蓝色,它会返回到原始状态,我如何通过单击名称来实现此目的?

代码

var c = $('<span><a class="internal_link" data-destination="' + this.Destination + '" data-id="' + this.Id + '"><span><input type="checkbox" class="multiselect-markets hidden" id="cb' + this.Id + '"><label for="cb' + this.Id + '" class="checkmark"></label></span></a></span>');
$.each(this.Markets, function() {

  b = $('<li  style="padding-left:28px"> <span style="color:#5a5959;">' + this.MarketName + '</span></li> ');
  b.append(c);
  list.append(b);
});

function test() {
  if ($(this.MarketName).click) {
    // im not sure in here how to tell it if the Name is clicked it should activate the checkbox and if its un clicked it should deselect the checkbox
  }
}
.checkmark {
  display: inline-block;
  width: 22px;
  /*height: 22px;*/
  height: 17px;
  -ms-transform: rotate(45deg);
  /* IE 9 */
  -webkit-transform: rotate(45deg);
  /* Chrome, Safari, Opera */
  transform: rotate(45deg);
  margin-left: 10%;
  margin-bottom: 1px;
}

.checkmark:before {
  content: '';
  position: absolute;
  width: 3px;
  height: 9px;
  background-color: #ccc;
  left: 11px;
  top: 6px;
}

.checkmark {
  cursor: pointer;
}

.checkmark:after {
  content: '';
  position: absolute;
  width: 3px;
  height: 3px;
  background-color: #ccc;
  left: 8px;
  top: 12px;
}

input[type="checkbox"]:checked+.checkmark:before,
input[type="checkbox"]:checked+.checkmark:after {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>NAME</span>
<span><a class="internal_market_link" data-destination="' + this.Destination + '" data-id="' + this.Id + '"><span><input type="checkbox" class="multiselect-markets" style="display:none;" id="cb' + this.Id + '"><label for="cb' + this.Id + '" class="checkmark"></label></span></a>
</span>

非常感谢您的指导

最佳答案

您可以更改您的 <span>NAME</span><label for="cb' + this.Id + '">NAME</label>

.checkmark {
    display: inline-block;
    width: 22px;
    /*height: 22px;*/
    height:17px;
    -ms-transform: rotate(45deg); /* IE 9 */
    -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
    transform: rotate(45deg);
    margin-left:10%;
    margin-bottom:1px;
}
.checkmark:before {
    content: '';
    position: absolute;
    width: 3px;
    height: 9px;
    background-color: #ccc;
    left: 11px;
    top: 6px;
}

.checkmark {
    cursor: pointer;
}

 .checkmark:after {
        content: '';
        position: absolute;
        width: 3px;
        height: 3px;
        background-color: #ccc;
        left: 8px;
        top: 12px;
    }

input[type="checkbox"]:checked + .checkmark:before,
input[type="checkbox"]:checked + .checkmark:after {
    background-color: blue;
}
<label for="cb' + this.Id + '">NAME</label>
<span><a class="internal_market_link" data-destination="' + this.Destination + '" data-id="' + this.Id + '"><span><input type="checkbox" class="multiselect-markets" style="display:none;" id="cb' + this.Id + '"><label for="cb' + this.Id + '" class="checkmark"></label></span></a></span>
                   
                  

关于jquery - 单击链接时如何选择和取消选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48557852/

相关文章:

javascript - 单击我的 jQuery 事件后如何移动其他 div?

java - 在 JTable 中显示 HTML

css - 如何在 JavaScript/CSS 中简单地创建一个图像反射效果?

javascript - 根据 Javascript,DIV 的宽度不是什么,为什么?

css - Visual Studio 代码中的语法错误 "expected [css-rcurlyexpected]"

javascript - 在Rails中,如何调用AJAX函数并在完成后刷新页面?

html - CSS - 如何在屏幕上显示分页符?

jquery - Bootstrap 多重选择下拉列表在附加时不起作用

jquery - 我试图在页面滚动 > 920 时显示 Logo

javascript - 退格时从输入文本动态增加字体大小