[英]Updating elements with jQuery selector
我遇到以下表单元素的情况:
<label for="edit-type-config-associated-element--2">Destination Data Element </label>
<input type="text" id="edit-type-config-associated-element--2"
name="type_config[associated_element]" value="23" size="60"
maxlength="128" class="form-text ajax-processed"
style="visibility: hidden;">
<input type="text" id="edit-type-config-associated-element--2_display"
value="23" size="60" maxlength="128"
class="form-text ajax-processed dropdowntree_aux dropdowntree_input valid"
readonly="" style="top: 61.515625px; left: 15px; position: absolute;
width: 389px; height: 16px;">
我想将更改侦听器添加到第二个输入元素(由edit-type-config-associated-element--2_display标识),然后更改第一个输入元素(由edit-type-config-associated-元素-2)。 这里要注意的是,每次显示此表单时,两个id末尾出现的数字都会加一(为什么他们不能使用一致的id,我不知道,但这就是我所坚持的)。
这是我用来尝试完成此操作的JavaScript:
// Add a change listener to associated element link for dc mappings
$('.dropdowntree_input').live('change', function () {
// Get the data element id
var dataElementID = $.trim($(this).val()),
dataElementLinks = Drupal.settings.data_element_links;
console.log('text field has changed to: ' + dataElementID);
if (dataElementLinks.hasOwnProperty(dataElementID)) {
$('#linked_form_elements').show();
} else {
$('#linked_form_elements').hide();
}
// Update the underlying text input to contain the right value.
$(':input[type="text"][name="type_config[associated_element]"]').val(dataElementID);
但是,这不起作用。 似乎在更新第二个文本字段时从未调用此函数。
有什么想法可以解决吗?
谢谢。
看起来您在选择器中缺少[
$('input[type="text"][name="type_config[associated_element]"]').val(dataElementID);
^ here
另外,如果您使用的是jQuery> = 1.9,则不再支持.live()。
如果您使用的是jQuery> = 1.7,请改用.on(),而且由于第一和第二输入是next / prev兄弟姐妹,因此您可以使用该关系来查找元素
$(document).on('change', '.dropdowntree_input', function () {
var dataElementID = $.trim($(this).val()),
dataElementLinks = Drupal.settings.data_element_links;
console.log('text field has changed to: ' + dataElementID);
if (dataElementLinks.hasOwnProperty(dataElementID)) {
$('#linked_form_elements').show();
} else {
$('#linked_form_elements').hide();
}
//since the hidden element is the previous sibling
$(this).prev().val(dataElementID);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.