0

我正在使用计算字段对重力形式进行一些测试,如下所示:

NumberA {9} - NumberB {11} 

结果立即出现,无需任何额外操作。

但我不能将此脚本分配给 HTML 字段类型,其行为是直接的,就像在上面的示例中一样:

<script>
gform.addFilter ('gform_calculation_result', function (result, FormulaField, formId, calcObj) {
     if (formulaField.field_id == "2") {
         var num = jQuery ('# input_6_1') val ().; 
         result = Math.pow (num, 3); 
     } 
     return result; 
}); 
</ script>

我已经读过,因为有像 Javascript keyup 这样的事件,但我不能让它工作,因为它不是一种占主导地位的语言。

我怎样才能得到第一个例子中的立竿见影的结果?

更新 1

我正在阅读有关如何进行回调的信息,并且已经找到了以下代码:

function do_a( callback ){ 
  gform.addFilter( 'gform_calculation_result', 
    function(result, formulaField, formId, calcObj ){     
      if ( formulaField.field_id == "2" ){ 
        var num = jQuery('#input_6_1').val(); 
        result = Math.pow(num, 3); 
      } 
      return result; 
  }); 
}
function do_b(){   
  console.log( '<code>do_b</code>: now we can make sure <code>do_b</code>'+
 ' comes out after <code>do_a</code>' ); 
}   
do_a( function(){   do_b(); }); 

但结果是一样的。我无法立即获得计算。

更新 2:

新尝试,但没有结果...

<script>
function f1( param1, callbackFunction ) {  
    alert( 'Started F1. \n\n Parameter: ' + param1);  
    callbackFunction ();  
}
f1 ( jQuery('#input_6_1').val(), function() {  
    alert( 'f1, execute.' );    
    gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){    
        if ( formulaField.field_id == "2" ){
            var num = jQuery('#input_6_1').val();
            result = Math.pow(num, 3);
        }
        f1 (result);
    });
});
</script>

更新 3:

这段代码来自html表单:

<body>
    <div id="preview_top">
        <div id="preview_hdr">
            <div><span class="actionlinks"><a href="javascript:window.close()" class="close_window">cerrar ventana</a></span>Previsualizar Formulario</div>
        </div>
        <div id="preview_note">Nota: Esta es una previsualización simple del formulario. Este formulario se puede ver de manera diferente cuando es agregado a una página del sitio con el estilo del mismo.</div>
    </div>
    <div id="preview_form_container">        
                <div class='gf_browser_chrome gform_wrapper' id='gform_wrapper_6' ><form method='post' enctype='multipart/form-data'  id='gform_6'  action='/wp/?gf_page=preview&amp;id=6'>
                        <div class='gform_heading'>
                            <h3 class='gform_title'>Exponent form FUNCIONANDO!!!</h3>
                            <span class='gform_description'></span>
                        </div>
                        <div class='gform_body'>
                            <ul id='gform_fields_6' class='gform_fields top_label description_below'><li id='field_6_1' class='gfield' ><label class='gfield_label' for='input_6_1'>Number ----  1</label><div class='ginput_container'><input name='input_1' id='input_6_1' type='text'  value='' class='medium' tabindex='1'   /></div></li><li id='field_6_2' class='gfield' ><label class='gfield_label' for='input_6_2'>Number ----  2</label><div class='ginput_container'><input name='input_2' id='input_6_2' type='text'  value='' class='medium' tabindex='2'  readonly="readonly" /></div></li><li id='field_6_3' class='gfield      gfield_html gfield_html_formatted gfield_no_follows_desc' ><script>
gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){    

        var num = jQuery('#input_6_1').val();
        result = Math.pow(num, 3);          
 $("input_6_2").html("the result is "+num);
});
</script></li>
                            </ul></div>
        <div class='gform_footer top_label'> <input type='submit' id='gform_submit_button_6' class='button gform_button' value='Enviar' tabindex='3' onclick='if(window["gf_submitting_6"]){return false;}  window["gf_submitting_6"]=true; '/>
            <input type='hidden' class='gform_hidden' name='is_submit_6' value='1' />
            <input type='hidden' class='gform_hidden' name='gform_submit' value='6' />
            <input type='hidden' class='gform_hidden' name='gform_unique_id' value='' />
            <input type='hidden' class='gform_hidden' name='state_6' value='WyJhOjA6e30iLCI3MTcyNDkwMWU1NWMxNWU4YmZiYTQ5MjY5M2U5ZjNhMiJd' />
            <input type='hidden' class='gform_hidden' name='gform_target_page_number_6' id='gform_target_page_number_6' value='0' />
            <input type='hidden' class='gform_hidden' name='gform_source_page_number_6' id='gform_source_page_number_6' value='1' />
            <input type='hidden' name='gform_field_values' value='' />

        </div>
                        </form>
                        </div><script type='text/javascript'> if(typeof gf_global == 'undefined') var gf_global = {"gf_currency_config":{"name":"D\u00f3lar Americano","symbol_left":"$","symbol_right":"","symbol_padding":"","thousand_separator":",","decimal_separator":".","decimals":2},"base_url":"http:\/\/localhost\/wp\/wp-content\/plugins\/gravityforms","number_formats":[],"spinnerUrl":"http:\/\/localhost\/wp\/wp-content\/plugins\/gravityforms\/images\/spinner.gif"};jQuery(document).bind('gform_post_render', function(event, formId, currentPage){if(formId == 6) {gf_global["number_formats"][6] = {"1":"decimal_dot","2":"decimal_dot","3":false};new GFCalc(6, [{"field_id":2,"formula":"{Number ----  2:2}","rounding":""}]);} } );jQuery(document).bind('gform_post_conditional_logic', function(event, formId, fields, isInit){} );</script><script type='text/javascript'> jQuery(document).ready(function(){jQuery(document).trigger('gform_post_render', [6, 1]) } ); </script>        </div>
            </body>

如果您看到代码,我有一个容器 id=input_6_2。我将此 id 与您建议的代码一起使用:

 $("input_6_2").html("the result is "+num);

当我使用 Javascript 控制台时,我在这一行收到以下错误:“Uncaught TypeError: undefined is not a function。

4

2 回答 2

0

您需要了解此类函数是异步的。我从没用过重力形式,但是 JavaScript 原理是一样的

此外,您还有几个语法问题,并且 JS 区分大小写,因此请正确设置大小写

gform.addFilter ('gform_calculation_result', 
  function (result, formulaField, formId, calcObj) { // is excuted AFTER the addFilter
    if (formulaField.field_id == "2") {
       var num = $('#input_6_1').val(); // not sure how you got your syntax
       result = Math.pow (num, 3); 
       $("#someResultContainer").html("the result is "+num);
    } 
   // return result; this is not useful here
 });  

更新

我相信您需要在 gform 插件触发后执行过滤器

就像是

$(function () {
    if (typeof gf_global == 'undefined') var gf_global = {
        "gf_currency_config": {
            "name": "D\u00f3lar Americano",
            "symbol_left": "$",
            "symbol_right": "",
            "symbol_padding": "",
            "thousand_separator": ",",
            "decimal_separator": ".",
            "decimals": 2
        },
        "base_url": "http:\/\/localhost\/wp\/wp-content\/plugins\/gravityforms",
        "number_formats": [],
        "spinnerUrl": "http:\/\/localhost\/wp\/wp-content\/plugins\/gravityforms\/images\/spinner.gif"
    };
    $(document).bind('gform_post_render', function (event, formId, currentPage) {
        if (formId == 6) {
            gf_global["number_formats"][6] = {
                "1": "decimal_dot",
                "2": "decimal_dot",
                "3": false
            };
            new GFCalc(6, [{
                "field_id": 2,
                "formula": "{Number ----  2:2}",
                "rounding": ""
            }]);
        }
    });
    $(document).bind('gform_post_conditional_logic', function (event, formId, fields, isInit) {

      gform.addFilter('gform_calculation_result', function(result, formulaField, formId, calcObj) {
        var num = jQuery('#input_6_1').val();
        var result = Math.pow(num, 3);
          window.console&&console.log(result);
        $("input_6_2").html("the result is " + num);
      });


    });
    $(document).trigger('gform_post_render', [6, 1]);
})
于 2014-05-20T11:49:53.997 回答
0

gform_calculation_result 过滤器仅在表单上存在具有活动计算功能的数字或产品字段时运行。

于 2014-05-20T14:54:35.807 回答