【问题标题】:Drupal.attachBehaviors not effect to returned ajax formDrupal.attachBehaviors 不影响返回的 ajax 表单
【发布时间】:2012-04-08 02:41:23
【问题描述】:

我正在开发 Drupal 7,我有一个这样构造的表单

function qt_debate_response_form($form, &$form_state, $node_id){
$form['node_id'] = array(
    '#type' => 'value',
    '#value' => $node_id,
);
$form['response_body'] = array(
    '#type' => 'textarea',
    '#required' => TRUE,
    '#row' => 4,
    '#default_value' => '',
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Post'),
    '#ajax' => array(
      'callback' => 'qt_debate_response_form_js',
      'wrapper' => 'response-message-' . $node_id,
      'method' => 'append',
      'effect' => 'fade',
    ),
);
return $form;
}

还有一个ajax回调函数来添加新评论

function qt_debate_response_form_js($form, $form_state) {
global $user;
$body_text = $form_state['values']['response_body'];
$node_id = $form_state['values']['node_id'];

$message_js = '
<script language="javascript" type="text/javascript">
    qt_debate_response_load_new_item(' . $node_id . ',' . $user->uid . ');

    jQuery(".response-form-wrapper textarea").val("");
</script>';


$comment = new stdClass();
$comment->nid = $form_state['values']['node_id']; // Node Id the comment will attached to
$comment->cid = 0;
$comment->pid = 0;
$comment->uid = $user->uid;
$comment->is_anonymous = 0;
$comment->homepage = '';
$comment->status = COMMENT_PUBLISHED;
$comment->language = LANGUAGE_NONE;
$comment->subject = text_summary($body_text, null, 60);
$comment->comment_body[$comment->language][0]['value'] = $body_text;
$comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
comment_submit($comment);
comment_save($comment);

$output = $message_js;

return $output;
}

这是我的 Javascript 将新创建的评论加载到 Div (ajax)

function qt_debate_user_post_load_new_items(debate_id) {
// get the latest comment id in context
$top_comment = jQuery(".view-debate-user-posts .views-row").first();
$top_comment_id = jQuery(".nid-field-hidden", $top_comment).html();

jQuery.ajax({
    type: "GET",
    url: "/qt_debate/ajax/load_new_items/" + debate_id + "/" + $top_comment_id,
    data: "",
    success: function(html){
        $new_items = jQuery(".view-content", html);
        jQuery("form", $new_items).attr("action","/debate/199");
        jQuery(".form-submit", $new_items).attr("id","edit-submit--5");

        if ($new_items.html() != null) {
            html = '<div class="new_items_wrapper" style="display: none">' + $new_items.html() + '</div>';
            if (jQuery(".view-debate-user-posts .view-content").length == 0) {
                jQuery(".view-debate-user-posts .view-empty").remove();
                jQuery(".view-debate-user-posts").append('<div class="view-content"></div>');
            }

            jQuery(".view-debate-user-posts .view-content").prepend(html);

            jQuery(".view-debate-user-posts .view-content .new_items_wrapper").fadeIn(500, function() {
                jQuery(".views-row", this).unwrap();
                Drupal.attachBehaviors();
            });
        }
    },
});

var t = setTimeout("qt_debate_user_post_load_new_items(" + debate_id + ")", 30000)
}

将视图内容返回给jQuery回调的hook_menu

function qt_debate_ajax_load_new_items() {
$debate_id = arg(3);

print views_embed_view('debate_user_posts_load_new_items_', 'default', array($debate_id));
exit(0);
}

查看模板文件,我里面也返回了一个新的表单

print drupal_render(drupal_get_form('qt_debate_response_form', $row->nid));

返回视图内容渲染良好,使用 Javascript 中的 Drupal.attachBehaviors,返回视图内容中的所有其他效果也可以正常工作。除了表单提交ajax。

有人可以帮忙吗? attachBehaviors 不适用于返回 ajax 表单。

非常感谢!

【问题讨论】:

    标签: ajax forms drupal drupal-behaviors


    【解决方案1】:
    Drupal.attachBehaviors(context);
    

    基本上重新运行由

    定义的任何函数
    Drupal.behaviors.yourFunctionName = function(context) {
        $('div.someSelectorclass:not(.already-processed-class)', context).addClass('already-processed-class').bind(someMethod);
    }
    

    并且这些方法必须添加一个选择器[already-processed-class]来测试是否bind(); [或点击(函数(e){});或每个(函数(){});或任何 ] 已经被添加。 “上下文”是传递小于“文档”的 - 例如,如果您的新内容已知位于原始行为函数仍可找到的较小上下文中:在此示例中,我可以传递我的父容器选择器新的'div.someSelectorclass'

    Drupal.attachBehaviors('div.parentContainerClass');
    

    而不是

    Drupal.attachBehaviors(document);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多