[英]Drupal disable button after clicking 3 times in drupal custom module
我是Drupal的新手
我构建了类似于webform的自定义模块,我的模块页面包含两个提交按钮和两个文本框,如下所示:
function contact_request($form, &$form_state) {
$form ['info'] =array(
'#markup' => "<div id='pageRequestDiv'>",
);
$form['number'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Number'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtli')),
'#required'=>true,
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'submit',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['verification_code'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtli')),
'#required'=>true,
);
$form['sendmail'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div></div>',
'#type' => 'submit',
'#value' => t('Send Request'),
'#ajax' => array(
'callback' => 'get_contact_us_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
return $form;
}
用户输入数字,然后第一个按钮将其发送到手机,然后在第二个文本框中输入短信,然后单击发送。
我想在用户单击3次后禁用第一个按钮。
我尝试了下面的JS,但是单击后禁用了两个按钮。 我只想在单击3次后禁用第一个按钮
Drupal.behaviors.hideSubmitButton = {
attach: function(context) {
$('form.node-form', context).once('hideSubmitButton', function () {
var $form = $(this);
$form.find('input.form-submit').click(function (e) {
var el = $(this);
el.after('<input type="hidden" name="' + el.attr('name') + '" value="' + el.attr('value') + '" />');
return true;
});
$form.submit(function (e) {
if (!e.isPropagationStopped()) {
$('input.form-submit', $(this)).attr('disabled', 'disabled');
return true;
}
});
});
}
};
更新的代码:
函数send_Verification_code_callback($ form,&$ form_state){一些发送短信的代码返回$ form; }
function contact_us_request($form, &$form_state) {
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
$form ['info'] =array(
'#markup' => "<div id='pageAbiliRequestDiv'>",
);
$form['contact_number'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Contact Number'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtabili')),
'#required'=>true,
);
$form['verification_code'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtabili')),
'#required'=>true,
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'submit',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'my-form-wrapper',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['sendmail'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div></div>',
'#type' => 'submit',
'#value' => t('Send Request'),
'#ajax' => array(
'callback' => 'get_contact_us_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['clicks'] = array(
'#type' => 'value',
);
if (isset($form_state['values']['clicks'])) {
if ($form_state['values']['clicks'] == 3) {
$form['send_vcode']['#disabled'] = TRUE;
} else {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
}
} else {
$form['clicks']['#value'] = 0;
}
我会使用AJAX回调而不是JavaScript检查。
用div包装整个表格:
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
并设置
$form['send_vcode'] = array(
...
'#ajax' => array(
'wrapper' => 'my-form-wrapper',
...
(并将您的消息区域也包含在表单中)。 在您的send_Verification_code_callback
函数中,返回整个表单。
然后,诀窍是将value
成分添加到包含点击次数的表单中:
$form['clicks'] = array(
'#type' => 'value',
);
if (isset($form_state['values']['clicks'])) {
if ($form_state['values']['clicks'] == 3) {
$form['send_vcode']['#disabled'] = TRUE;
} else {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
}
} else {
$form['clicks']['#value'] = 0;
}
3单击send_vcode
按钮后,它将被禁用。
===更新===
以下是有效的代码(没有所有不必要的内容),显示了pageAbiliRequestDiv
div中剩余的点击量:
function contact_us_request($form, &$form_state) {
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
$form['clicks'] = array(
'#type' => 'value',
);
$clicks_max = 3;
if (isset($form_state['values']['clicks'])) {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
$clicks_left = $clicks_max - 1 - $form_state['values']['clicks'];
} else {
$form['clicks']['#value'] = 0;
$clicks_left = $clicks_max;
}
$form ['info'] =array(
'#markup' => '<div id="pageAbiliRequestDiv">'
. t('Clicks left: @clicks_left', array('@clicks_left' => $clicks_left))
. '</div>',
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'button',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'my-form-wrapper',
'method'=>'replace',
'effect'=>'fade',
),
);
if ($clicks_left == 0) {
$form['send_vcode']['#disabled'] = TRUE;
}
return $form;
}
function send_Verification_code_callback($form, &$form_state) {
return $form;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.