【发布时间】:2020-06-16 02:20:52
【问题描述】:
我知道这已经被问过了。我检查了有关此主题的所有先前问题,但找不到解决方案。
这是问题所在:我有两个输入,一个是复选框,另一个是具有三种可能选择的收音机。在函数内部,我必须首先查看是否选中了第一个复选框,如果是,我将使用 if else 语句检查其他一些内容,否则该函数将继续执行。无线电输入稍后将出现在同一函数中。这将检查三个选项中的哪一个先前已被选中,并将设置一个变量等于选中的选项的值。要查看复选框是否被选中,我使用带有 .is(':checked') 的 jQuery,但它每个都返回 false,即使我选中了它们。有什么想法吗?
对不起,如果我没有正确使用 Stack Overflow,但这是我的第一个问题。
这是 HTML,input 是 #geoloc_waypoint_active,收音机是 #locomotion_radio
<div id="create_route_modal_content" class="modal-body">
<div id="geo_switch">
<div id="geoSwitchDiv">
<label for="geoloc_waypoint_active">
Usa la tua posizione
</label>
<label class="switch">
<input id="geoloc_waypoint_active" class="form-check form-check-inline" type="checkbox">
<span class="slider round"></span>
</label>
</div>
<br>
<div id="locomotion_radio">
<label><input class="locomInput" type="radio" name="locomotion" value="walking" checked><img class='locomotionImg' src='immagini/walking.png'></label>
<label><input class="locomInput" type="radio" name="locomotion" value="cycling"><img class='locomotionImg' src='immagini/cycling.png'></label>
<label><input class="locomInput" type="radio" name="locomotion" value="driving"><img class='locomotionImg' src='immagini/driving.png'></label>
</div>
DrawOnMap() {
let formattedCoord = "";
let geoposition = $('#geoloc_waypoint_active').is(':checked');
console.log(geoposition)
if (geoposition) {
var geoL = $('#geo_Locator .mapboxgl-ctrl .mapboxgl-ctrl-icon');
if (!map.getLayer('points') && geoL.attr('aria-pressed') === 'false') {
alert("L'utente non ha una posizione attualmente attiva.");
return;
} else {
this.waypoints.unshift(window.userPosition);
}
}
if (this.waypoints.length < 2) {
alert("Devi inserire almeno due punti di interesse.");
return;
}
this.waypoints.forEach((waypoint, index, source) => {
formattedCoord += waypoint[0] + "," + waypoint[1];
if (index < source.length - 1) {
formattedCoord += ";";
}
});
let locomotion = $('input[name=locomotion]:checked').val();
let geoposition = $('#geoloc_waypoint_active').is(':checked'); 总是 false 所以它永远不会进入 if
与let locomotion = $('input[name=locomotion]:checked').val();相同,它找不到选中的并设置运动
【问题讨论】:
-
你什么时候调用你的 DrawOnMap() 函数?因为我刚试过,如果你把它绑定到复选框的 onchange 事件上,.is(':checked') 返回正确的结果
-
我想到的唯一可能的问题是您在选中复选框之前调用了该函数。您可以尝试调用 $('#geoloc_waypoint_active').change(function() { DrawOnMap(); });
-
console.log($('#geoloc_waypoint_active').length)给你什么? -
@freedomn-m 它打印 1
-
抱歉,永远是 1,试试:
console.log($('[id=geoloc_waypoint_active]').length)- 如果不是 1,那就是你的问题。
标签: javascript jquery html