gpt4 book ai didi

javascript - HTML 输入复选框始终返回 false

转载 作者:行者123 更新时间:2023-12-01 05:12:21 25 4
gpt4 key购买 nike

我知道已经有人问过这个问题了。我检查了之前有关该主题的所有问题,但找不到解决方案。

这就是问题:我有两个输入,一个是复选框,另一个是具有三种可能选择的单选按钮。在函数内部,我必须首先查看第一个复选框是否被选中,如果是,我将使用 if else 语句检查其他一些内容,否则函数将继续。 radio 输入稍后将出现在同一功能内。该选项将检查先前检查过三个选项中的哪一个,并将设置一个等于所检查选项的值的变量。要查看该复选框是否已选中,我使用 jQuery 和 .is(':checked') ,但它每次都会返回 false ,即使我检查过它们。有什么想法吗?

抱歉,如果我没有正确使用 Stack Overflow,但这是我的第一个问题。

这是 HTML,input#geoloc_waypoint_active radio 是 #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(); 相同找不到选中的并设置运动

最佳答案

您是否尝试过这个$("#geoloc_waypoint_active")[0].checked它将为您提供控制正确的值。

$(document).ready(function(){
console.log($("#geoloc_waypoint_active")[0].checked)
$('#check').on('click',function(){
console.log($("#geoloc_waypoint_active")[0].checked)
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input value='checkVakue' type='button' id='check'/>
<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>

关于javascript - HTML 输入复选框始终返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60511695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com