我試圖獲取存儲在隱藏輸入字段中的值。 '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標籤中的隱藏輸入嗎?
使用'$(event.target).closest(」行包裝 ')找到(' hiddenID ').val();' – Satpal 2014-11-24 10:34:28
@Satpal感謝您的回答,但我仍然得到'未定義'的id值。所以,當你使用表格時,父母絕對不能訪問div標籤? – 2014-11-24 10:38:16
如果'update'('(this),'time');'like $('input [name = time]'),你需要傳遞'event'。 (事件,'時間'); ......' – Satpal 2014-11-24 10:40:55