[英]how can we show/hide div tags using value of radio buttons in jquery or javascript?
hi i am very new to jquery and i am working on show and hide div tags using values of radio buttons. 嗨,我是jquery的新手,我正在使用单选按钮的值来显示和隐藏div标签。 here is my HTML. 这是我的HTML。
<div id="OwnVehicle" style="display:none"> <label> <input type="radio" name="vehicleList" id="two" onclick="fuelList()" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" onclick="fuelList()" value="2" />Four Wheeler </label> </div> <div id="TwoWheeler" style="display:none"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler" style="display:none"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
now i need help in writing a jquery code for showing the div tag with id = "two wheeler" when radio button value = 1 and div tag with id = "four wheeler" when radio button value = 2. 现在,我需要帮助编写一个jquery代码,以在单选按钮值= 1时显示id =“ two wheeler”的div标签,在单选按钮值= 2时显示id =“ four wheeler”的div标签。
thnaks in advance. 提前打招呼。
sorry for anyone who couldnt understand my question. 对不起任何无法理解我的问题的人。
Here is what you can do easily with JQuery 这是您可以轻松使用JQuery进行的操作
$(document).ready(function(){ $("input[name='vehicleList']").click(function(){ var selectedValue = $(this).val(); if(selectedValue === "1"){ $('#TwoWheeler').show(); $('#FourWheeler').hide(); }else{ $('#TwoWheeler').hide(); $('#FourWheeler').show(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="OwnVehicle"> <label> <input type="radio" name="vehicleList" id="two" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" value="2" />Four Wheeler </label> </div> <div id="TwoWheeler" style="display:none"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler" style="display:none"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
My first suggestion is to change your onclick="" to onchange="". 我的第一个建议是将您的onclick =“”更改为onchange =“”。
<input type="radio" name="vehicleList" id="four" onchange="fuelList(this)" value="2" class="radioButtons" />Four Wheeler
<script>
function fuelList(element)
{
var id = element.value;
if( id == 1)
{
$(".wheels")hide(); //hides all the elements with class = wheels
$("#twoWheelers")show(); //shows the div with id = twoWheelers
}
else if( id == 2 )
{
$(".wheels")hide(); //hides all the elements with class = wheels
$("#fourWheelers")show(); //shows the div with id = fourWheelers
}
}
</script>
and put a class to the div of your "twoWheeler" and "fourWheeler" 并在“ twoWheeler”和“ fourWheeler”的div上添加一个类
I just make it "wheels" 我只是把它做成“轮子”
<div id="twoWheelers" class="wheels">
...
</div>
<div id="fourWheelers" class="wheels">
...
</div>
Yiu can try this working code: Yiu可以尝试以下工作代码:
function fuelList(_this){ var value=$(_this).attr("value"); $("#TwoWheeler").hide(); $("#FourWheeler").hide(); if(value=="1"){ $("#TwoWheeler").show(); } else if(value=="2"){ $("#FourWheeler").show(); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="OwnVehicle"> <label> <input type="radio" name="vehicleList" id="two" onclick="fuelList(this)" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" onclick="fuelList(this)" value="2" />Four Wheeler </label> </div> <div id="TwoWheeler" style="display:none"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler" style="display:none"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
try this one 试试这个
function fuelList(val) { var val = parseInt(val); if (val == 1) { $('#FourWheeler').hide(); $('#TwoWheeler').show(); } else { $('#TwoWheeler').hide(); $('#FourWheeler').show(); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="OwnVehicle"> <label> <input type="radio" name="vehicleList" id="two" onclick="fuelList(this.value)" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" onclick="fuelList(this.value)" value="2" />Four Wheeler </label> </div> <div id="TwoWheeler" style="display:none"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler" style="display:none"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
Assuming I understood the question correctly, and you want to hide a div when you check a radio button? 假设我正确理解了这个问题,并且您想在选中单选按钮时隐藏div?
Here is an example of how that can be achieved: 这是一个如何实现的示例:
HTML: HTML:
<input type="radio" name="euResident" id="euResidentYes" value="Yes" checked />
<label for="euResidentYes" class="radio">yes</label>
<input type="radio" name="euResident" id="euResidentNo" value="No" />
<label for="euResidentNo" class="radio">no</label>
<div id='div'>Hello World</div>
Javascript: Javascript:
function switchVar() {
if($("#euResidentNo").prop("checked"))
$("#div").show();
else
$("#div").hide();
}
$(document).ready(function() {
switchVar();
$("input[name=euResident]").change(switchVar);
});
It is an example using Javascript as this is perfect to use for this. 这是一个使用Javascript的示例,因为它非常适合用于此目的。 Demo: http://jsfiddle.net/2Uj9F/169/ 演示: http : //jsfiddle.net/2Uj9F/169/
Here you go with jsfiddle https://jsfiddle.net/ym10add9/ 在这里您使用jsfiddle https://jsfiddle.net/ym10add9/
$("input[type='radio']").change(function() {
$('div').hide();
$( '#' + $(this).attr('value')).show();
});
I wrote code as per you describe in your post. 我按照您在帖子中的描述编写了代码。 May be useful to solve your problem. 可能对解决您的问题很有用。
$(document).ready(function() { $('input[type=radio][name=vehicleList]').change(function() { if (this.value == '1') { $("#FourWheeler").hide(); $("#TwoWheeler").show(); } else if (this.value == '2') { $("#FourWheeler").show(); $("#TwoWheeler").hide(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="vehicleList"> <label> <input type="radio" name="vehicleList" id="two" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" value="2" />Four Wheeler </label> </div> <h5>Output : </h5> <div id="TwoWheeler" style="display:none"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler" style="display:none"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
Below is simple JQuery code that will help you to achieve your requirement: 以下是简单的JQuery代码,可帮助您实现要求:
$("input[name='vehicleList']").on('click', function() { if ($(this).val() == 1) { $('#TwoWheeler').show(); $('#FourWheeler').hide(); } else { $('#TwoWheeler').hide(); $('#FourWheeler').show(); } });
#TwoWheeler, #FourWheeler { display: none; } #OwnVehicle label, #TwoWheeler label, #FourWheeler label { cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="OwnVehicle"> <label> <input type="radio" name="vehicleList" id="two" value="1" />Two Wheeler </label> <label> <input type="radio" name="vehicleList" id="four" value="2" />Four Wheeler </label> </div> <div id="TwoWheeler"> <label> <input type="radio" name="TwoWheeler" value="6" />Electric </label> <label> <input type="radio" name="TwoWheeler" value="7" />Petrol </label> </div> <div id="FourWheeler"> <label> <input type="radio" name="FourWheeler" value="1" />Electric </label> <label> <input type="radio" name="FourWheeler" value="2" />Petrol </label> <label> <input type="radio" name="FourWheeler" value="3" />Diesel </label> <label> <input type="radio" name="FourWheeler" value="4" />CNG </label> <label> <input type="radio" name="FourWheeler" value="5" />LPG </label> </div>
This is one way to do it: 这是一种方法:
$( "#r0" ).bind( "click", function() {
$("#OwnVehicle").show();
$("#TwoWheeler").hide();
});
$( "#r1" ).bind( "click", function() {
$("#OwnVehicle").hide();
$("#TwoWheeler").show();
});
Here a working fiddle https://jsfiddle.net/0aa8w2sj/ 这是一个工作的小提琴https://jsfiddle.net/0aa8w2sj/
It can be improved 可以改善
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.