最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Cannot prefill hidden Ninja Forms fields

programmeradmin4浏览0评论

If have a page where you can select a specific broker from a list. The broker name has to be filled in into the hidden field in Ninja Forms.

if(jQuery('#broker-list').length) { //checks if the list exists
    jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
        var broker_name = "";

        jQuery(document).on("click", ".broker" , function() {
            broker_name = jQuery('.broker__name', this).text();

            console.log(broker_name); // this works perfectly

            if(jQuery('#nf-field-33').length) {
                jQuery(this).val(broker_name); //value of field is still empty
            }
            if(jQuery('#nf-field-34').length) {
                jQuery(this).val(broker_name); //value of field is still empty
            }
        });
    });
}

I always thought that you have to wrap is with jQuery(document).on( 'nfFormReady', function( e, layoutView ) { }); in order to prefill fields in Ninja Forms with jQuery. But that doesn't seems to work in this case. What wrong over here?

I know there are PHP possibilities to fill in data in a field, but that isn't an option this case.

It works perfectly when I copy/paste the code above in console.

If have a page where you can select a specific broker from a list. The broker name has to be filled in into the hidden field in Ninja Forms.

if(jQuery('#broker-list').length) { //checks if the list exists
    jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
        var broker_name = "";

        jQuery(document).on("click", ".broker" , function() {
            broker_name = jQuery('.broker__name', this).text();

            console.log(broker_name); // this works perfectly

            if(jQuery('#nf-field-33').length) {
                jQuery(this).val(broker_name); //value of field is still empty
            }
            if(jQuery('#nf-field-34').length) {
                jQuery(this).val(broker_name); //value of field is still empty
            }
        });
    });
}

I always thought that you have to wrap is with jQuery(document).on( 'nfFormReady', function( e, layoutView ) { }); in order to prefill fields in Ninja Forms with jQuery. But that doesn't seems to work in this case. What wrong over here?

I know there are PHP possibilities to fill in data in a field, but that isn't an option this case.

It works perfectly when I copy/paste the code above in console.

Share Improve this question asked Jan 15, 2020 at 8:31 DennisDennis 1358 bronze badges 2
  • I'd try moving the outermost check into the nfFormReady handler, i.e. don't check #broker-list until everything is ready. Or wrap the whole thing in a jQuery(document).ready(...). – Rup Commented Jan 15, 2020 at 13:55
  • were you able to solve this? – Al Rosado Commented Apr 29, 2020 at 15:58
Add a comment  | 

1 Answer 1

Reset to default 1

According to https://developer.ninjaforms/codex/changing-field-values/ you need to add .trigger( 'change' ) when changing Ninfa Forms field values programmatically.

This should work:

if(jQuery('#broker-list').length) { //checks if the list exists
    jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
        var broker_name = "";

        jQuery(document).on("click", ".broker" , function() {
            broker_name = jQuery('.broker__name', this).text();

            console.log(broker_name); // this works perfectly

            if(jQuery('#nf-field-33').length) {
                jQuery(this).val(broker_name).trigger( 'change' );
            }
            if(jQuery('#nf-field-34').length) {
                jQuery(this).val(broker_name).trigger( 'change' );
            }
        });
    });
}
发布评论

评论列表(0)

  1. 暂无评论