"Simple" jQuery 给定多个相同 ID 的遍历难度(我知道)
"Simple" jQuery traversal difficulty given multiple identical ID's (I know)
这应该很简单,但我脑子一片空白。鉴于我想在标记中其上方的输入发生变化时更新 input#mirror-field
- 我如何定位 "next" ID?
鉴于(不要问!)页面上还有其他具有相同 ID 的输入。
所以简单来说,我只想遍历到具有该 ID 的 下一个 输入,而不是其他输入。
<div class="controls">
<input type="text" class="form_datetime" value="11/09/16">
<input type="hidden" id="mirror_field" name="nextcontact" value="07/04/17">
<input type="hidden" name="currentnextcontact" value="2016-09-11">
</div>
当前 jQuery:
$(".form_datetime").change(function() {
var $value = $(this).val();
$('#mirror_field').val($value); //Which I assume will affect all the #mirror_field inputs on the page (I know, I know)
});
您可以使用jQuery的.siblings()
功能:
$(".form_datetime").change(function() {
var $value = $(this).val();
$(this).siblings('#mirror_field').val($value);
// This will select the "mirror_field" element that are siblings
// of the "form_datetime" element that just changed.
});
这应该很简单,但我脑子一片空白。鉴于我想在标记中其上方的输入发生变化时更新 input#mirror-field
- 我如何定位 "next" ID?
鉴于(不要问!)页面上还有其他具有相同 ID 的输入。
所以简单来说,我只想遍历到具有该 ID 的 下一个 输入,而不是其他输入。
<div class="controls">
<input type="text" class="form_datetime" value="11/09/16">
<input type="hidden" id="mirror_field" name="nextcontact" value="07/04/17">
<input type="hidden" name="currentnextcontact" value="2016-09-11">
</div>
当前 jQuery:
$(".form_datetime").change(function() {
var $value = $(this).val();
$('#mirror_field').val($value); //Which I assume will affect all the #mirror_field inputs on the page (I know, I know)
});
您可以使用jQuery的.siblings()
功能:
$(".form_datetime").change(function() {
var $value = $(this).val();
$(this).siblings('#mirror_field').val($value);
// This will select the "mirror_field" element that are siblings
// of the "form_datetime" element that just changed.
});