2014-11-24 74 views
-1

我试图获取存储在隐藏输入字段中的值。 'onchange'或'onfocusout'事件我正在调用JavaScript函数,但我无法在'id'变量中获得正确的值。使用jquery父函数获取隐藏输入值

这是我的HTML:

<div class="row-wrapper"> 
    <input type="hidden" name="hiddenID" class="hiddenID" value="1" /> 
    <table class="default-table"> 

     <tr> 
      <th>Client <em>*</em> 
      </th> 
      <th>Project <em>*</em> 
      </th> 
      <th>Category <em>*</em> 
      </th> 
      <th>Description</th> 
      <th class="small">Time <em>*</em> 
      </th> 
      <th class="small">Overtime</th> 
     </tr> 
     <tr> 
      <td> 
       <select name="client" class='client' onchange='return update(event, "client");'> 
        <option>Choose client</option> 
        <option>Client 1</option> 
        <option>Client 2</option> 
       </select> 
      </td> 
      <td> 
       <select name="project" class='project' onchange='return update(event, "project");'> 
        <option>Choose project</option> 
        <option>Project 1</option> 
        <option>Project 2</option> 
       </select> 
      </td> 
      <td> 
       <select> 
        <option>Choose category</option> 
        <option>Front-End Development</option> 
        <option>Design</option> 
       </select> 
      </td> 
      <td> 
       <input type="text" class="in-text medium" name="description"/> 
      </td> 
      <td class="small"> 
       <input type="text" class="in-text xsmall" name="time" /> 
      </td> 
      <td class="small"> 
       <input type="text" class="in-text xsmall" name="time" /> 
      </td> 
     </tr> 
</div> 

这是我的脚本现在:

<script type="text/javascript"> 
     $('input[name=description]').focusout(function() { 
      update($(this), 'description'); 
     }); 

     $('input[name=time]').focusout(function() { 
      update($(this), 'time'); 
      calculate(); 
     }); 

     function update(event, type) { 
      var id = $(event.target).parent().find('.hiddenID').val(); 
      var this_element = $(event.target).val(); 
      alert('id: ' + id + ' this element val: ' + this_element + ' this element is: ' + type); 

      $(event.target).parent().find('.id').val(); 

      return false; 
     } 
</script> 

什么是jQuery中使用父()函数的正确方法?我正在阅读文档,因为我明白parent()只能找到父div标签。有一些其他功能可以与表格一起工作,并找到与表格相同的div标签中的隐藏输入吗?

+1

使用'$(event.target).closest(”行包装 ')找到(' hiddenID ').val();' – Satpal 2014-11-24 10:34:28

+0

@Satpal感谢您的回答,但我仍然得到'未定义'的id值。所以,当你使用表格时,父母绝对不能访问div标签? – 2014-11-24 10:38:16

+0

如果'update'('(this),'time');'like $('input [name = time]'),你需要传递'event'。 (事件,'时间'); ......' – Satpal 2014-11-24 10:40:55

回答

1

问题是与你的update()函数:

新代码:。

function update(event, type) { 
     var id = $(event.target).closest('.row-wrapper').find('.hiddenID').val(); 
     var this_element = $(event.target).val(); 
     alert('id: ' + id + ' this element val: ' + this_element + ' this element is: ' + type); 

     $(event.target).parent().find('.id').val(); 

     return false; 
    } 
1

把在引号:$( '输入[名称= “ID”]')

或$( '[名称= “ID”]')VAL();