简体   繁体   English

CSS 根据 HTML 选择选项值选择另一个元素

[英]CSS to select another Element based on a HTML Select Option Value

Is there a CSS selector that allows me to select an element based on an HTML select option value?是否有 CSS 选择器允许我根据 HTML 选择选项值选择元素?

<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<p>Display only if an option with value 1 is selected</p>

I'm looking for an HTML/CSS only method to only display a selected number of form fields.我正在寻找一种仅 HTML/CSS 的方法来仅显示选定数量的表单字段。 I already know how to do it with Javascript.我已经知道如何用 Javascript 做到这一点。

Is there a way to do this?有没有办法做到这一点?


Edit:编辑:

The question title is perhaps misleading.问题标题可能具有误导性。 I'm not trying to style the select box, that's pretty common and plenty of answers on SO already.我不是要设计选择框的样式,这很常见,而且已经有很多关于 SO 的答案。 I'm was actually trying to style the <P> element based on the value selected in the <select> .我实际上是在尝试根据<select>的值来设置<P>元素的样式。

How ever what I'm really trying to do is to display a number of form fields based on a selected numeric value:我真正想做的是根据选定的数值显示多个表单字段:

<select name="number-of-stuffs">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<div class="stuff-1">
     <input type="text" name="stuff-1-detail">
     <input type="text" name="stuff-1-detail2">
</div>
<div class="stuff-2" style="display:none"> 
     <input type="text" name="stuff-2-detail">
     <input type="text" name="stuff-2-detail2">
</div>
<div class="stuff-3" style="display:none">
     <input type="text" name="stuff-3-detail">
     <input type="text" name="stuff-4-detail2">
</div>

I would like to display div.stuff-1 and div.stuff-2 when number-of-stuffs=2 and display div.stuff-1 div.stuff-2 and div.stuff-3 when number of stuffs=2.我想在 number-of- display div.stuff-1 = div.stuff-2时显示div.stuff-1div.stuff-2在 number-of- display div.stuff-1 =2 时display div.stuff-1 div.stuff-2div.stuff-3

Something like this fiddle像这个小提琴一样的东西

Its called an attribute selector它称为属性选择器

option[value="1"]
{
background-color:yellow;
} 

Example http://jsfiddle.net/JchE5/示例http://jsfiddle.net/JchE5/

You can use您可以使用

select option[value="1"]

but browser support won't be fantastic.但浏览器支持不会很棒。

This probably requires a parent selector which has not been specified for CSS2 or CSS3.这可能需要一个尚未为 CSS2 或 CSS3 指定的父选择器

A subject selector has been defined in the CSS4 working draft as of May 2013 but no browser vendor has implemented it yet.主题选择器2013 年 5 月的 CSS4 工作草案中定义,但尚无浏览器供应商实施它。

Whether the method in question works (in theory) using the subject selector remains to be seen.所讨论的方法是否(理论上​​)使用主题选择器仍然有待观察。

How about the alternative below?下面的替代方案怎么样?

You don't get a select box, but perhaps this is close enough.您没有select框,但也许这已经足够接近了。

 #option-1, #option-2, #option-3 { display: none; } #nos-1:checked ~ #option-1, #nos-2:checked ~ #option-2, #nos-3:checked ~ #option-3 { display: block; }
 <input id="nos-1" name="number-of-stuffs" type="radio" value="1" checked="checked" /><label for="nos-1">1</label> <input id="nos-2" name="number-of-stuffs" type="radio" value="2" /><label for="nos-2">2</label> <input id="nos-3" name="number-of-stuffs" type="radio" value="3" /><label for="nos-3">3</label> <div id="option-1"> <input type="text" name="stuff-1-detail" value="1-1" /> <input type="text" name="stuff-1-detail2" value="1-2" /> </div> <div id="option-2"> <input type="text" name="stuff-2-detail" value="2-1" /> <input type="text" name="stuff-2-detail2" value="2-2" /> </div> <div id="option-3"> <input type="text" name="stuff-3-detail" value="3-1" /> <input type="text" name="stuff-4-detail2" value="3-2" /> </div>

i believe that in "live" or realtime, it is possible only with javascript or jQuery.我相信在“实时”或实时中,只有使用 javascript 或 jQuery 才有可能。 Wrap the potentially hidden fields in divs with display: none onLoad and have JS or jQuery change state to display: block or however you like.display: none onLoad 将潜在隐藏的字段包装在 div 中,并让 JS 或 jQuery 更改状态以display: block或您喜欢的任何内容。

//Show Hide based on selection form Returns
$(document).ready(function(){

//If Mobile is selected
$("#type").change(function(){
    if($(this).val() == 'Movil'){
     $("#imeiHide").slideDown("fast"); // Slide down fast
    } else{
     $("#imeiHide").slideUp("fast"); //Slide Up Fast
    }
});

//If repairing is selected
$("#type").change(function(){
if($(this).val() == 'repairing'){


$("#problemHide").slideDown("fast"); // Slide down fast
$("#imeiHide").slideDown("fast"); // Slide down fast
}else{
$("#problemHide").slideUp("fast"); //Slide Up Fast
}
});
});
// type is id of <select>
// Movil is option 1
// Repairing is option 2
//problemHide is div hiding field where we write problem
//imeiHide is div hiding field where we write IMEI

i am using in one of my apps...我在我的一个应用程序中使用...

Possible with PHP too, but with PHP, you will have to send the change request or you can write a code in php to have certain classes to be loaded based on already selected values, for example也可以使用 PHP,但使用 PHP,您必须发送更改请求,或者您可以在 php 中编写代码以根据已选择的值加载某些类,例如

<select class="<?php if($valueOne == 'Something'){echo 'class1';}else{echo 'class2';}">
   <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

I found a solution based on this answer satisfies op's request most.我发现基于此答案的解决方案最能满足操作员的要求。 Not purely in CSS but definitely least amount of JavaScript is involved.不纯粹是在 CSS 中,但绝对涉及最少的 JavaScript。

 select[data-chosen='1']~.stuff-1{ display: block !important; } select:not([data-chosen='1'])~.stuff-1{ display: none; } select[data-chosen='2']~.stuff-2{ display: block !important; } select[data-chosen='3']~.stuff-3{ display: block !important; }
 <select name="number-of-stuffs" data-chosen = "1" onchange = "this.dataset.chosen = this.value;"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <div class="stuff-1"> <span> I am stuff-1!</span> <input type="text" name="stuff-1-detail"> <input type="text" name="stuff-1-detail2"> </div> <div class="stuff-2" style="display:none"> <span> I am stuff-2!</span> <input type="text" name="stuff-2-detail"> <input type="text" name="stuff-2-detail2"> </div> <div class="stuff-3" style="display:none"> <span> I am stuff-3!</span> <input type="text" name="stuff-3-detail"> <input type="text" name="stuff-4-detail2"> </div>

You don't even need to write any separated js, despite embedding "this.dataset.chosen = this.value;"尽管嵌入了"this.dataset.chosen = this.value;" ,您甚至不需要编写任何单独的js "this.dataset.chosen = this.value;" in onchange seems to be a bit hacky.onchange似乎有点hacky。

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

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