[英]How can I check which radio button is selected and apply an appropriate behaviour with if else statements in jQuery
这是我的代码 :
http://codepen.io/ekilja01/full/KaMXjp/
我正在尝试检测选择了哪个单选按钮,然后显示适当的输出,无论是摄氏度还是华氏度作为最终输出。
这是我的HTML :
<body>
<div class="container-fluid">
<i class="fa fa-thermometer-empty fa-2x" aria-hidden="true"></i>
<br><br><br>
<form>
<input type="radio" name="celsiusOrFahrenheit" value="f" id="fahrenheit"> Fahrenheit °F;<br>
<input type="radio" name="celsiusOrFahrenheit" value="c" id ="celsius" > Celsius °C<br>
</form>
<div class="yourlocation">
<h1>Your location is: </h1>
<p class="yourLocationGoesHere">
</p>
</div>
<h1>Your current weather is: </h1>
<div class="showTemperature">
<p class="showDegree"></p>
</div>
<div class="icon">
</div>
</div>
</body>
这是我的jQuery:
$(document).ready(function(){
$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6", function (data){
var len = data.length;
var location = "";
var temperature = "";
var celsius = Math.floor(data.main.temp - 273.15);
var fahrenheit = Math.floor(9/5 * (data.main.temp - 273) + 32);
function checkSelection(){ if(document.getElementById('celsius').checked) {
//Celsius radio button is checked
return celsius;
}else if(document.getElementById('fahrenheit').checked) {
//Fahrenheit radio button is checked
return fahrenheit;
} else {
return "Please select Fahrenheit or Celsius"
}
};
location += "<p>'" + data.name + ", " + data.sys.country + "'</p>";
temperature += "<p>'" + checkSelection() + "'</p>";
$(".yourLocationGoesHere").html(location);
$(".showDegree").html(temperature);
});
});
请帮忙。
你需要听取无线电的改变事件,并在事件处理程序的条件下做你想做的任何事情
$(':radio').change(function(){
// "this" will be the checked radio element
if (this.id === 'celsius'){
// code you want for celsius
}else{
// code you want for fahrenheit
}
});
change
单选按钮时,需要attach
change
事件处理程序。
此外,您必须在外部声明celsius
和fahrenheit
变量,以便在change
方法中访问它们。
$(document).ready(function(){ var celsius=""; var fahrenheit=""; $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6", function (data){ var len = data.length; var location = ""; celsius = Math.floor(data.main.temp - 273.15); fahrenheit = Math.floor(9/5 * (data.main.temp - 273) + 32); location += "<p>'" + data.name + ", " + data.sys.country + "'</p>"; $(".yourLocationGoesHere").html(location); }); $('input[type="radio"]').change(function(){ temperature = ""; if(document.getElementById('celsius').checked) { temperature += "<p>'" + celsius+ "'</p>"; $(".showDegree").html(temperature); }else if(document.getElementById('fahrenheit').checked) { temperature += "<p>'" + fahrenheit+ "'</p>"; $(".showDegree").html(temperature); } else { return "Please select Fahrenheit or Celsius" } }); });
@import url('https://fonts.googleapis.com/css?family=Press+Start+2P'); body { max-width: 42em; padding: 2em; margin: 5px auto; color: #161616; font-family: 'Press Start 2P', cursive; text-align: center; background-color: currentColor; } h1 { line-height: 45px; color: #fff; font-weight: lighter; font-size: 1em; font-family: 'Press Start 2P', cursive; text-align: center; text-transform: uppercase; } p { color: #fff; } form { color: #fff; text-align:center !important; } i { color: #fff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://use.fontawesome.com/43f8201759.js"> </script> <body> <div class="container-fluid"> <i class="fa fa-thermometer-empty fa-2x" aria-hidden="true"></i> <br><br><br> <form> <input type="radio" name="celsiusOrFahrenheit" value="f" id="fahrenheit"> Fahrenheit °F;<br> <input type="radio" name="celsiusOrFahrenheit" value="c" id ="celsius"> Celsius °C<br> </form> <div class="yourlocation"> <h1>Your location is: </h1> <p class="yourLocationGoesHere"> </p> </div> <h1>Your current weather is: </h1> <div class="showTemperature"> <p class="showDegree"></p> </div> <div class="icon"> </div> </div> </body>
$(document).ready(function(){ $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6", function (data){ var len = data.length; var location = ""; var temperature = ""; var celsius = Math.floor(data.main.temp - 273.15); var fahrenheit = Math.floor(9/5 * (data.main.temp - 273) + 32); function checkSelection(){ if(document.getElementById('celsius').checked) { //Celsius radio button is checked return celsius; }else if(document.getElementById('fahrenheit').checked) { //Fahrenheit radio button is checked return fahrenheit; } else { return "Please select Fahrenheit or Celsius" } }; function update(){ var loc = "<p>'" + data.name + ", " + data.sys.country + "'</p>"; temperature += "<p>'" + checkSelection() + "'</p>"; $(".yourLocationGoesHere").html(loc); $(".showDegree").html(temperature); } update(); $('input[type=radio][name="celsiusOrFahrenheit"]').on('change', update); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid"> <i class="fa fa-thermometer-empty fa-2x" aria-hidden="true"></i> <form> <input type="radio" name="celsiusOrFahrenheit" value="f" id="fahrenheit"> Fahrenheit °F;<br> <input type="radio" name="celsiusOrFahrenheit" value="c" id ="celsius" > Celsius °C<br> </form> <div class="yourlocation"> <h1>Your location is: </h1> <p class="yourLocationGoesHere"> </p> </div> <h1>Your current weather is: </h1> <div class="showTemperature"> <p class="showDegree"></p> </div> <div class="icon"> </div> </div>
add == true,你准备好了
的document.getElementById( '摄氏')。检查==真
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.