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

标签 javascript jquery html

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

这就是问题:我有两个输入,一个是复选框,另一个是具有三种可能选择的单选按钮。在函数内部,我必须首先查看第一个复选框是否被选中,如果是,我将使用 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/

相关文章:

javascript - MeteorJS 和 AmplifyJS 响应式(Reactive)

javascript - 推送多项选择的数据值 仅多次推送第一个单击的选项

Javascript 标签导航

javascript - 如何在jQuery中使用Goto语句?

javascript - React Native - 不变违规 "RNDateTimePicker"

javascript - 如何在 javascript 中对用户函数进行自定义回调

javascript - Internet Explorer 9 及以下版本,在主体上使用 rtl 方向时将滚动条设置为右侧

javascript - 错误函数中的 JQuery AJAX HTML 响应

javascript - 在滚动部分时更改固定 div 的类

jquery - 导航附加背景图像更改