Javascript 如何检查选中了哪个单选按钮,并对jQuery中的if-else语句应用适当的行为
这是我的代码笔:Javascript 如何检查选中了哪个单选按钮,并对jQuery中的if-else语句应用适当的行为,javascript,jquery,html,if-statement,Javascript,Jquery,Html,If Statement,这是我的代码笔: <body> <div class="container-fluid"> <i class="fa fa-thermometer-empty fa-2x" aria-hidden="true"></i> <br><br><br> <form> <input t
<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>
我正在尝试检测选择了哪个单选按钮,然后显示相应的输出,无论是摄氏度还是华氏度,作为最终输出
这是我的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>
华氏度和摄氏度;F
摄氏度和摄氏度;C
您的位置是:
你现在的天气是:
这是我的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);
});
});
$(文档).ready(函数(){
$.getJSON(“http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6“,功能(数据){
var len=data.length;
var位置=”;
var-temperature=“”;
变化摄氏度=数学地板(data.main.temp-273.15);
var fahrenheit=数学地板(9/5*(data.main.temp-273)+32);
函数checkSelection(){if(document.getElementById('Celsium')。选中){
//选中“摄氏度”单选按钮
返回摄氏度;
}else if(document.getElementById('fahrenheit')。选中){
//已选中华氏单选按钮
返回华氏温度;
}否则{
return“请选择华氏或摄氏度”
}
};
位置+=“”“+data.name+”,“+data.sys.country+””;
温度+=“”“+checkSelection()+””;
$(“.yourlocationgoesher”).html(位置);
$(“.showdegate”).html(温度);
});
});
请帮助。add==true,您已经准备好了 document.getElementById('celsius')。checked==true
$(文档).ready(函数(){
$.getJSON(“http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6“,功能(数据){
var len=data.length;
var位置=”;
var-temperature=“”;
变化摄氏度=数学地板(data.main.temp-273.15);
var fahrenheit=数学地板(9/5*(data.main.temp-273)+32);
函数checkSelection(){if(document.getElementById('Celsium')。选中){
//选中“摄氏度”单选按钮
返回摄氏度;
}else if(document.getElementById('fahrenheit')。选中){
//已选中华氏单选按钮
返回华氏温度;
}否则{
return“请选择华氏或摄氏度”
}
};
函数更新(){
var loc=“”“+data.name+”,“+data.sys.country+””;
温度+=“”“+checkSelection()+””;
$(“.yourlocationgoesher”).html(loc);
$(“.showdegate”).html(温度);
}
更新();
$('input[type=radio][name=“celsiusOrFahrenheit”]')。在('change',update');
});
});代码>
华氏度和摄氏度;F
摄氏度和摄氏度;C
您的位置是:
你现在的天气是:
您需要监听收音机的更改事件,并在事件处理程序的条件范围内执行您想要的任何操作
$(':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
}
});
更改单选按钮时,您需要附加一个更改
事件处理程序
此外,您必须在外部声明摄氏
和华氏
变量,以便在更改
方法中访问它们
$(文档).ready(函数(){
var摄氏=”;
var fahrenheit=“”;
$.getJSON(“http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=7f5806c8f3fd28b03e2d6580a50732d6“,函数(数据){
var len=data.length;
var位置=”;
摄氏度=数学地板(data.main.temp-273.15);
华氏温度=数学楼层(9/5*(data.main.temp-273)+32);
位置+=“”“+data.name+”,“+data.sys.country+””;
$(“.yourlocationgoesher”).html(位置);
});
$('input[type=“radio”]”)。更改(函数(){
温度=”;
if(document.getElementById('celsius')。选中){
温度+=“”“+摄氏度+””;
$(“.showdegate”).html(温度);
}else if(document.getElementById('fahrenheit')。选中){
温度+=“”“+fahrenheit+””;
$(“.showdegate”).html(温度);
}否则{
return“请选择华氏或摄氏度”
}
});
});代码>
@导入url('https://fonts.googleapis.com/css?family=Press+启动+2P');
身体{
最大宽度:42em;
填料:2米;
保证金:5px自动;
颜色:#161616;
字体系列:“按开始2P”,草书;
文本对齐:居中;
背景色:当前颜色;
}
h1{
线高:45px;
颜色:#fff;
字体重量:较轻;
字号:1em;
字体系列:“按开始2P”,草书;
文本对齐:居中;
文本转换:大写;
}
p{
颜色:#fff;
}
形式{
颜色:#fff;
文本对齐:居中!重要;
}
我{
颜色:#fff;
}
华氏度和摄氏度;F
摄氏度和摄氏度;C
您的位置是:
你现在的天气是:
具体问题是什么?您提到了一个目标,但没有提到代码与预期的不同之处。当我在华氏温度或摄氏温度之间切换时,它在您当前的WHEATER is下不会显示:我的意思是它必须检测用户希望以何种格式获得最终输出并显示它。If语句不能做到这一点。如果您尝试选择华氏温度或摄氏温度,则不会进行切换。您需要一个更改事件侦听器,以确保@charlietfl是正确的。您编写的代码仅在页面成功加载后执行一次。您需要一段代码,它在每次输入更改时都会运行。看看$