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

时间:2019-09-14 08:10:30

标签: javascript ajax select this

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

<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>

预先感谢

2 个答案:

答案 0 :(得分:1)

您可以通过在函数中传递事件参数来获取它。

<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>

希望这会有所帮助。 谢谢。

答案 1 :(得分:0)

我已通过所有帮助以这种形式解决了该问题 所有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>