$(this) 表单的值 select 具有许多具有相同字段名称的表单

$(this) value of form select with many forms with same field names

我的页面上有很多具有相同字段的不同表单 我喜欢检索当前值并将其打印到页面上。 现在我得到了第一个表单的值,但我喜欢从表单(我更改的那个)中检索当前值

<FORM method=post id=form name=form class=form>
<SELECT id=cal_starttime name=cal_starttime onchange=optellen()>
<OPTION>10:00</OPTION>
<OPTION>11:00</OPTION>
<input type=submit value=Save class=submit id=submit>
</FORM>

<script>
optellen = function() {
  var starttime = document.getElementById("cal_starttime").value;
  var endtime = document.getElementById("cal_endtime").value;
  document.getElementById("sumuur").innerHTML = (endtime-starttime);
}
</script>

提前致谢

在函数中传入event参数即可获取

<FORM method=post id=form name=form class=form>
    <SELECT id=cal_starttime name=cal_starttime onchange="optellen(event)">
        <OPTION>10:00</OPTION>
        <OPTION>11:00</OPTION>
        <input type=submit value=Save class=submit id=submit>
    </select>

    <SELECT id="cal_endtime" name="cal_endtime">
        <OPTION>10:00</OPTION>
        <OPTION>11:00</OPTION>
        <input type=submit value=Save class=submit id=submit>
    </select>
</FORM>

<script>
    optellen = function(event) {
        var stime = event.target.id;
      var starttime = document.getElementById(stime).value;
        console.log(starttime);
      var endtime = document.querySelector("#cal_endtime").value;
      console.log(endtime);

      //document.getElementById("sumuur").innerHTML = (endtime-starttime);
    }
</script>

希望这对您有所帮助。 谢谢

我已经通过此表格中的所有帮助解决了它 现在所有 id 都是唯一的并且可以从 e.

$count++
<FORM method=post id=form name=form class=form>
    <SELECT id=cal_starttime_$count name=cal_starttime onchange="optellen($count)">
        <OPTION>10:00</OPTION>
        <OPTION>11:00</OPTION>
        <input type=submit value=Save class=submit id=submit>
    </select>

    <SELECT id="cal_endtime_$count" name="cal_endtime" onchange="optellen($count)>
        <OPTION>10:00</OPTION>
        <OPTION>11:00</OPTION>
        <input type=submit value=Save class=submit id=submit>
        </SELECT>
        <span id=sumuur_$count></span>
</FORM>




<script>
optellen = function(erbij) {
  var starttime = document.getElementById('cal_starttime_'+erbij+'').value;
  var endtime = document.getElementById('cal_endtime_'+erbij+'').value;

  document.getElementById('sumuur_'+erbij+'').innerHTML = starttime ;
// alert(starttime);
}
</script>