【问题标题】:How to place a tag element between the input tag and the hidden field?如何在输入标签和隐藏字段之间放置标签元素?
【发布时间】:2017-05-03 12:39:18
【问题描述】:

我在一个项目中使用了Spring表单标签库,遇到了如下问题:

我需要在标签上调用一个 JavaScript 函数 onmouseup 到一个复选框。

问题在于 Spring 表单:checkbox 标记输出然后看起来像这样

<input id="id" name="name" value="true" type="checkbox">
<input name="_name" value="on" type="hidden"> 
<label for="id" onmouseup="func()" >blabla</label>

由于我的 style.css 使用 + 选择器在选中复选框后获取标签,我需要类似:

<input id="id" name="name" value="true" type="checkbox">
<label for="id" onmouseup="func()" >blabla</label>
<input name="_name" value="on" type="hidden"> 

但是因为我想在标签上调用onmouseup函数,所以form:checkbox标签的label属性也不起作用。

我的输入被一个 div 包围,所以基本上使用 form:checkbox 上的 label 属性和 div 上的 onmouseup 调用会起作用,但感觉有点 hacky。

所以总结一下,我需要一种不同的方式来让我的 CSS 工作,或者需要一种使用 Spring 实现上述 HTML 结构的方式。

CSS(其余的只是样式,不应该很重要,但如果对我有帮助,我会发布它):

.switch input[type="checkbox"]:checked + label:after{
    left: 23px;
}

.switch input[type="checkbox"]:checked + label{
    background-color: #fff;
}

【问题讨论】:

  • .switch input[type="checkbox"]:checked + input + label:after{ left: 23px; } .switch input[type="checkbox"]:checked + input + label{ background-color: #A539BD;也不起作用:S
  • Marco,我有点困惑.. 你想保留这个 HTML 结构并使用 CSS 来在选中复选框后在标签中添加一些样式吗?

标签: css spring checkbox label


【解决方案1】:

如果我正确理解了您的问题,以下是您的解决方案:https://jsfiddle.net/DiogoBernardelli/rLutkcx0/

我在复选框和标签之间添加了input[type="hidden"]

代码如下:

.switch input[type="checkbox"]:checked + input[type="hidden"] + label:after{
    left: 23px;
}

.switch input[type="checkbox"]:checked + input[type="hidden"] + label{
    background-color: #fff;
    color: #f00; //just to emphasize the label
}

【讨论】:

  • 如果对您没有帮助,请添加评论,我会尽力帮助您。
  • 不,这是为我做的,非常感谢 - 但为什么它只适用于 [type="hidden"] 而没有它(请参阅我的第一条评论问题):S
  • 如果您想在没有隐藏字段的情况下执行此操作或将其移动到标签标记之后,只需在我们的解决方案中删除“+ input[type="hidden"]”即可。你可以在这里了解这个“+”选择器:stackoverflow.com/questions/1139763/…
  • 隐藏的输入字段是由 spring 标签生成的,所以它必须在那里:S ;D
  • 明白了。所以,你必须使用“+”选择器才能让它工作。您也可以尝试“>”选择器并从代码中删除“input[type="hidden"]”,也许它也可以。顺便说一句,别忘了对我的主要答案投赞成票哈哈。
猜你喜欢
  • 2015-01-16
  • 2021-11-22
  • 1970-01-01
  • 2011-07-25
  • 2022-01-03
  • 2011-12-20
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
相关资源
最近更新 更多