[英]Updating elements with jQuery selector
I have a situation where I have the following form elements: 我遇到以下表单元素的情况:
<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;">
I would like to add a change listener to the second input element (identified by edit-type-config-associated-element--2_display) and then change the input in the first input element (identified by edit-type-config-associated-element--2). 我想将更改侦听器添加到第二个输入元素(由edit-type-config-associated-element--2_display标识),然后更改第一个输入元素(由edit-type-config-associated-元素-2)。 The catch here is that everytime this form is displayed the number appearing at the end of both ids increments by one (why they couldn't use a consistent id, I have no idea but it is what I am stuck with).
这里要注意的是,每次显示此表单时,两个id末尾出现的数字都会加一(为什么他们不能使用一致的id,我不知道,但这就是我所坚持的)。
Here is the javascript I am using to try and accomplish this: 这是我用来尝试完成此操作的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);
However this is not working. 但是,这不起作用。 It seems that this function is never called when the second textfield is updated.
似乎在更新第二个文本字段时从未调用此函数。
Any ideas where I have gone off the rails? 有什么想法可以解决吗?
Thanks. 谢谢。
Looks like you were missing a [
in your selector 看起来您在选择器中缺少
[
$('input[type="text"][name="type_config[associated_element]"]').val(dataElementID);
^ here
Also if you are using jQuery >= 1.9 then .live() is no longer supported. 另外,如果您使用的是jQuery> = 1.9,则不再支持.live()。
If you are using jQuery >= 1.7 then use .on() instead, also since the first and second inputs are next/prev siblings you can use that relationship to find the element 如果您使用的是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.