在Drupal自定义模块中点击3次后,Drupal禁用按钮
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){发送短信的一些代码return$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;
}
单击send_vcode
按钮3次后,它将被禁用。
===更新===
这是工作代码(没有所有不必要的东西),它显示了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;
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 多个单选按钮组相互干扰
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- Javascript按钮下拉列表
- jquery试图按名称获取按钮位置
- 漂亮照片图片库中的Facebook赞按钮
- 扩展移相器按钮类不工作
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 单击按钮以等待单击按钮
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何从querySelectorAll中获取按钮类型
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 按下按钮时保存cookie
- 是否可以禁用jquery中的单个单选按钮
- 在Drupal自定义模块中点击3次后,Drupal禁用按钮
- 阻止Drupal's在表单API中单击按钮后刷新的页面
- 点击按钮并在Drupal 7上保存cookie
- 将页面中的CSS(或页面元素)替换为按钮(Drupal)