Drupal CCK add another item callback
I'm trying to get the last field item to focus when doing a 'add another item' on a CCK multiple value field.
Here's my code:
$("#node-form table.content-multiple-table tr.draggable input[type='text']").live("keydown", function (e) {
if (e.which == 13) {
$(this).closest("table.content-multiple-table").siblings("div.content-add-more").find("input.form-submit").mousedown();
$(this).closest("tr.draggable").next().find("input[type='text']").trigger("focus");
return false;
}
});
Clearly this 开发者_开发问答doesn't work because the field is created on AHAH response. :(
Anyone know how to hook into the response to focus the last field? Thanks.
You should use Drupal.behaviors and Drupal should manage attaching the behaviors for you. Modules should re run these after modifying the DOM (including AHAH requests).
In your case you will probably want to add two behaviors, one to the button to flag that it was pressed and another on the last field to focus if the flag is set (and unset the flag).
Looking at the source code of cck/includes/content.node_form.inc, and more precisely this part in the content_add_more_js function:
// Build our new form element for the whole field, asking for one more element.
$form_state['item_count'] = array($field_name => count($_POST[$field_name]) + 1);
$form_element = content_field_form($form, $form_state, $field);
// Let other modules alter it.
drupal_alter('form', $form_element, array(), 'content_add_more_js');
I'd say you'll need to setup a hook_form_alter where the form_id is 'content_add_more_js', as it seems this is called after generating the new field, but before adding it to the page.
I haven't tried that yet though...
Oussama Mubarak // Semiaddict
Use last: http://api.jquery.com/last
I was having a similar issue (in D6; might be different for D7). The key is replacing
jQuery(document).ready(function(){
//your stuff happens
});
with:
Drupal.behaviors.myModuleBehavior = function (context) {
//your stuff happens
};
精彩评论