我正在尝试获取存储在隐藏输入字段中的值。 'onchange' 或 'onfocusout' 事件 我正在调用 JavaScript 函数,但我无法在 'id' 变量中获取正确的值。
这是我的 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() 函数的正确方法是什么?我正在阅读文档,据我所知, parent() 只找到父 div 标签。是否有其他一些函数可以处理表格并找到与表格位于同一 div 标记中的隐藏输入?
最佳答案
问题出在你的 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;
}
关于javascript - 使用 jquery 父函数获取隐藏输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102674/