在 Ninja Forms 中至少强制使用两个表单字段之一 [英] Making at least one of two form fields mandatory in Ninja Forms

查看:56
本文介绍了在 Ninja Forms 中至少强制使用两个表单字段之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 WordPress 中的 ninja 表单插件,我目前将姓名、电子邮件、电话作为表单字段.我想要它,所以名称是必需的,并且填写电子邮件或电话,以便提交表格而不会出错.这可能吗?

Using the ninja forms plugin in WordPress, I currently have Name, Email, Phone as the form fields. I would like it so the Name is required, and either email OR phone be filled out for the form to submit without an error. Is this possible?

我在他们的文档中没有看到任何地方提到这一点,如果无法通过插件选项实现,我也在考虑使用 jQuery.

I don't see anything on their docs that mention this anywhere, I am also considering using jQuery for this if its not possible via the plugin options.

推荐答案

由于没有人对此作出回复,我决定使用 jQuery.我花了一段时间才弄清楚您需要在 nfFormReady 上运行您的函数.

Since there were no replies on this I decided to use jQuery. Took me a while to figure out you need to run your function on nfFormReady.

它检查字段 1 或字段 2 是否有值,如果有则启用提交按钮,如果没有则禁用提交按钮.这不是最优雅的解决方案,但可以满足我的需要.除了下面的代码之外,我还添加了一个星号,并在填写时在另一个有条件的必填字段中将其删除.

It checks to see if field 1 OR field 2 has a value, if so then it enables the submit button, if not the submit button is disabled. It's not the most elegant solution but is working for what I need. Also beyond the below code, I added in an asterisk's and remove it on the other conditionally required field when one is being filled out.

$(document).on( 'nfFormReady', function() { // this is important

    // settings: for 2 dynamic-required fields
    var formID = '2'; // form ID
    var fieldID1 = '9'; // field ID one
    var fieldID2 = '12'; // field ID two
    var submitID = '11'; // Submit button ID

    $('#nf-form-' + formID + '-cont #nf-field-' + submitID + '-wrap input').attr('disabled', true).css({"background": "#cccccc", "cursor": "default"}); // disable submit button

    // on keypress/change enable submit button
    $('#nf-form-' + formID + '-cont input').on('keyup keypress focusout change', function() {
        if(($('#nf-form-' + formID + '-cont #nf-field-' + fieldID1 + '-wrap input').val()) || ($('#nf-form-' + formID + '-cont #nf-field-' + fieldID2 + '-wrap input').val())){
          $('#nf-form-' + formID + '-cont #nf-field-' + submitID + '-wrap input').attr('disabled', false).css({"background": "", "cursor": "pointer"}); // enable submit button
        }else{
          $('#nf-form-' + formID + '-cont #nf-field-' + submitID + '-wrap input').attr('disabled', true).css({"background": "#cccccc", "cursor": "default"}); // disable submit button
        };
    });
});

这篇关于在 Ninja Forms 中至少强制使用两个表单字段之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆