【问题标题】:How to get closest input type text's value in javascript?如何在javascript中获取最接近的输入类型文本值?
【发布时间】:2018-05-05 06:57:02
【问题描述】:

我正在用 html 设计一个表格,它在一个 td 中包含 time picker 作为开始时间,在第二个 td 中包含第二个时间选择器作为一个 tr 中的结束时间。我想同时取两个值(开始时间和结束时间)并想计算时间差并想在第三个 td 中显示。但我无法获得特定的 td 值来计算差异。

  <table id="example1" class="machinetable" cellspacing="0" width="100%">
  <thead>
    <tr>

     <th>Sr. no.</th>
     <th>Machine Name</th>
     <th >Rate Type</th>
     <th >Rate Unit</th>
     <th >Rate</th>
     <th >Start Time</th>
     <th >End Time</th>
     <th >Total Time</th>
 </thead>
 <tbody>
    <tr role="row" class="odd">
      <td>1</td>
      <td>JCB</td>
      <td>MH23GH5477</td>
      <td>per Hour</td>
      <td>1000</td>
      <td>
          <div class="input-group"><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
        </div>
      </td>
      <td>
         <div class="form-group nomargin">
            <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this.value)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
        </div>
      </td>
      <td class="total_time"></td>
   </tr>
   <tr role="row" class="odd">
      <td>2</td>
      <td>JCB</td>
      <td>MH2398</td>
      <td>per Hour</td>
      <td>1000</td>
      <td>
         <div class="input-group" ><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
         </div>
      </td>
      <td>
         <div class="form-group nomargin">
            <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this.value)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
         </div>
      </td>
      <td class="total_time"></td>
   </tr>
</tbody>

Javascript 函数:

function calculate_time(time)
{
   var endtime1 = time;
   var starttime1= 
   $(this).closest('td').prev('td').find("input[name=start_time]")val();
   alert(starttime1);
   alert(endtime1);
}

出现错误:

开始时间未定

我是 js 新手。

【问题讨论】:

  • find 后少了一个点
  • 谢谢大家的回复..但我仍然得到同样的错误。我使用的表是数据表。
  • 你能解开一个 jsfiddle 以便我测试它。很难重构依赖于第三方工具(如 jquery 和 datepickers)的问题

标签: javascript jquery html jquery-selectors


【解决方案1】:

由于您在函数内使用当前元素的引用,因此首先在函数中传递this 而不是this.value。您还错过了val() 之前的点 (.)。

现在改变如下函数:

function calculate_time(time){
  var endtime1 = time.value;
  var starttime1= $(time).closest('td').prev('td').find('.start_time').val();
  alert(starttime1);
  alert(endtime1);
}

function calculate_time(time){
   var endtime1 = time.value;
   var starttime1= $(time).closest('td').prev('td').find('.start_time').val();
   alert(starttime1);
   alert(endtime1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="example1" class="machinetable" cellspacing="0" width="100%">
  <thead>
    <tr>

     <th>Sr. no.</th>
     <th>Machine Name</th>
     <th >Rate Type</th>
     <th >Rate Unit</th>
     <th >Rate</th>
     <th >Start Time</th>
     <th >End Time</th>
     <th >Total Time</th>
 </thead>
 <tbody>
    <tr role="row" class="odd">
      <td>1</td>
      <td>JCB</td>
      <td>MH23GH5477</td>
      <td>per Hour</td>
      <td>1000</td>
      <td>
          <div class="input-group"><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
        </div>
      </td>
      <td>
         <div class="form-group nomargin">
            <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
        </div>
      </td>
      <td class="total_time"></td>
   </tr>
   <tr role="row" class="odd">
      <td>2</td>
      <td>JCB</td>
      <td>MH2398</td>
      <td>per Hour</td>
      <td>1000</td>
      <td>
         <div class="input-group" ><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
         </div>
      </td>
      <td>
         <div class="form-group nomargin">
            <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
         </div>
      </td>
      <td class="total_time"></td>
   </tr>
</tbody>

【讨论】:

    【解决方案2】:

    您的代码中有几个问题:

    1. 您可以在calculate_time() 函数中传递元素的引用,这样您就可以使用该元素来查找它附近的其他元素。目前calculate_time() 中的$(this) 引用的是Window 对象,而不是您的实际元素。
    2. calculate_time() 函数中传递元素引用后使用$(elem).closest('td').prev('td').find("input[name=start_time]").val()

    以下是获取 start_timeend_time 值的工作示例,您仍然需要将其与日期字段集成,并在使用以下代码获取日期值后计算差异:

    function calculate_time(elem)
    {
       var endtime1 = elem.value;
       var starttime1 = $(elem).closest('td').prev('td').find("input[name=start_time]").val();
       alert(starttime1);
       alert(endtime1);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="example1" class="machinetable" cellspacing="0" width="100%">
      <thead>
        <tr>
    
         <th>Sr. no.</th>
         <th>Machine Name</th>
         <th >Rate Type</th>
         <th >Rate Unit</th>
         <th >Rate</th>
         <th >Start Time</th>
         <th >End Time</th>
         <th >Total Time</th>
     </thead>
     <tbody>
        <tr role="row" class="odd">
          <td>1</td>
          <td>JCB</td>
          <td>MH23GH5477</td>
          <td>per Hover</td>
          <td>1000</td>
          <td>
              <div class="input-group"><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
            </div>
          </td>
          <td>
             <div class="form-group nomargin">
                <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
            </div>
          </td>
          <td class="total_time"></td>
       </tr>
       <tr role="row" class="odd">
          <td>2</td>
          <td>JCB</td>
          <td>MH2398</td>
          <td>per Hover</td>
          <td>1000</td>
          <td>
             <div class="input-group" ><input name="start_time" value="" class="form-control form-cont datetime start_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
             </div>
          </td>
          <td>
             <div class="form-group nomargin">
                <div class="input-group" ><input name="end_time" value="" onchange="calculate_time(this)" class="form-control form-cont datetime end_time" type="text"><span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
             </div>
          </td>
          <td class="total_time"></td>
       </tr>
    </tbody>

    【讨论】:

      【解决方案3】:

      在调用类似 onchange="calculate_time(this.value, '1')" 这样的函数时再传递一个参数 其中 1 是 start_time 的 id 如下所示

      <input name="start_time" id="start_time1" value="" class="form-control form-cont datetime start_time" type="text">
      

      那么你的函数看起来像:

      <script>
      function calculate_time(time, id)
      {
      //alert(id);
         var endtime1 = time;
         var starttime1= $("#start_time"+id).val();
      
         alert(endtime1);
         alert(starttime1);
      }
      </script>
      

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

        试试这个 这里没有进行验证。

          <table id="example1" class="machinetable" cellspacing="0" width="100%">
          <thead>
            <tr>
        
             <th>Sr. no.</th>
             <th>Machine Name</th>
             <th >Rate Type</th>
             <th >Rate Unit</th>
             <th >Rate</th>
             <th >Start Time</th>
             <th >End Time</th>
             <th >Total Time</th>
         </thead>
         <tbody>
            <tr role="row" class="odd">
              <td>1</td>
              <td>JCB</td>
              <td>MH23GH5477</td>
              <td>per Hour</td>
              <td>1000</td>
              <td>
                  <div class="input-group">
                  
                  <input name="start_time" value="" class="form-control form-cont datetime start_time" type="text" id="time_a_1">
                  
                  <span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
                </div>
              </td>
              <td>
                 <div class="form-group nomargin">
                    <div class="input-group" >
                    <input name="end_time" value="" onkeyup="calculate_time(1)" class="form-control form-cont datetime end_time" type="text" id="time_a_2">
                    <span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
                </div>
              </td>
              <td class="total_time" id="time_a"></td>
           </tr>
           <tr role="row" class="odd">
              <td>2</td>
              <td>JCB</td>
              <td>MH2398</td>
              <td>per Hour</td>
              <td>1000</td>
              <td>
                 <div class="input-group" >
                 <input name="start_time" value="" class="form-control form-cont datetime start_time" type="text" id="time_b_1">
                 <span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span>
                 </div>
              </td>
              <td>
                 <div class="form-group nomargin">
                    <div class="input-group" >
                    <input name="end_time" value="" onkeyup="calculate_time(2)" class="form-control form-cont datetime end_time" type="text" id="time_b_2">
                    <span class="input-group-addon group-addon"><span><i class="fa fa-clock-o" aria-hidden="true"></i></span></span></div>
                 </div>
              </td>
              <td class="total_time" id="time_b"></td>
           </tr>
        </tbody>
        
        <script>
        function calculate_time(time)
        {
        if(time == 1){
        start_time = document.getElementById("time_a_1").value
        end_time = document.getElementById("time_a_2").value
        total = start_time + end_time
        document.getElementById("time_a").innerText = total;
        }
        else if(time == 2){
        start_time = document.getElementById("time_b_1").value
        end_time = document.getElementById("time_b_2").value
        total = start_time + end_time
        document.getElementById("time_b").innerText = total;
        }
        }
        </script>

        【讨论】:

          猜你喜欢
          • 2012-08-02
          • 2017-02-01
          • 1970-01-01
          • 2019-02-13
          • 1970-01-01
          • 2013-11-23
          • 2015-03-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多