如何在重力表单单选按钮中添加onclick动作

时间:2018-07-21 22:32:02

标签: javascript jquery

我正在尝试使用重力形式按钮字段,以便当用户选择一个选项时,它会转到下一页而无需用户单击下一步。我在这里进行了快速搜索,发现以下代码:

// This is the initial GravityForms binding, it will be lost upon a page change with next/previous
// Thus we make a bind on gform_page_loaded event also
 if( jQuery('.custom-form').length > 0 ) {
	jQuery('.gfield_radio input[type=radio]').bind("click", function() {
		//console.log('Clicked: ' + jQuery( this ).closest('.gform_page').find('.gform_page_footer .gform_next_button.button') );
		jQuery(this).closest('.gform_page').find('.gform_page_footer .gform_next_button.button').click();
	});
}

jQuery(document).bind('gform_page_loaded', function(event, form_id, current_page){
      // code to be trigger when next/previous page is loaded
      if( jQuery('.custom-form').length > 0 ) {
		jQuery('.gfield_radio input[type=radio]').bind("click", function() {
			//console.log('Clicked: ' + jQuery( this ).closest('.gform_page').find('.gform_page_footer .gform_next_button.button') );
			jQuery(this).closest('.gform_page').find('.gform_page_footer .gform_next_button.button').click();
		});
	}
  });
https://gist.github.com/yanknudtskov/a82d1f3fcfaca171ae1f14b74c65f2de

但是在我的表单上不起作用:

<form method="post" enctype="multipart/form-data" id="gform_5" action="/test/#gf_5">
        <div id="gf_progressbar_wrapper_5" class="gf_progressbar_wrapper">
            <h3 class="gf_progressbar_title">Step 1 of 2
        </h3>
            <div class="gf_progressbar">
                <div class="gf_progressbar_percentage percentbar_blue percentbar_50" style="width:50%;"><span>50%</span></div>
            </div></div>
                        <div class="gform_body"><div id="gform_page_5_1" class="gform_page">
                                    <div class="gform_page_fields"><ul id="gform_fields_5" class="gform_fields top_label form_sublabel_below description_below"><li id="field_5_1" class="gfield field_sublabel_below field_description_below gfield_visibility_visible"><label class="gfield_label">Untitled</label><div class="ginput_container ginput_container_radio"><ul class="gfield_radio" id="input_5_1"><li class="gchoice_5_1_0"><input name="input_1" type="radio" value="First Choice" id="choice_5_1_0"><label for="choice_5_1_0" id="label_5_1_0">First Choice</label></li><li class="gchoice_5_1_1"><input name="input_1" type="radio" value="Second Choice" id="choice_5_1_1"><label for="choice_5_1_1" id="label_5_1_1">Second Choice</label></li><li class="gchoice_5_1_2"><input name="input_1" type="radio" value="Third Choice" id="choice_5_1_2"><label for="choice_5_1_2" id="label_5_1_2">Third Choice</label></li></ul></div></li></ul>
                    </div>
                    <div class="gform_page_footer">
                         <input type="button" id="gform_next_button_5_2" class="gform_next_button button" value="Next" onclick="jQuery(&quot;#gform_target_page_number_5&quot;).val(&quot;2&quot;);  jQuery(&quot;#gform_5&quot;).trigger(&quot;submit&quot;,[true]); " onkeypress="if( event.keyCode == 13 ){ jQuery(&quot;#gform_target_page_number_5&quot;).val(&quot;2&quot;);  jQuery(&quot;#gform_5&quot;).trigger(&quot;submit&quot;,[true]); } "> 
                    </div>
                </div>
                <div id="gform_page_5_2" class="gform_page" style="display:none;">
                    <div class="gform_page_fields">
                        <ul id="gform_fields_5_2" class="gform_fields top_label form_sublabel_below description_below"><li id="field_5_3" class="gfield field_sublabel_below field_description_below gfield_visibility_visible"><label class="gfield_label">Untitled</label><div class="ginput_container ginput_container_radio"><ul class="gfield_radio" id="input_5_3"><li class="gchoice_5_3_0"><input name="input_3" type="radio" value="First Choice" id="choice_5_3_0"><label for="choice_5_3_0" id="label_5_3_0">First Choice</label></li><li class="gchoice_5_3_1"><input name="input_3" type="radio" value="Second Choice" id="choice_5_3_1"><label for="choice_5_3_1" id="label_5_3_1">Second Choice</label></li><li class="gchoice_5_3_2"><input name="input_3" type="radio" value="Third Choice" id="choice_5_3_2"><label for="choice_5_3_2" id="label_5_3_2">Third Choice</label></li></ul></div></li>
                            </ul></div>
        <div class="gform_page_footer top_label"><input type="submit" id="gform_previous_button_5" class="gform_previous_button button" value="Previous" onclick="if(window[&quot;gf_submitting_5&quot;]){return false;}  window[&quot;gf_submitting_5&quot;]=true;  " onkeypress="if( event.keyCode == 13 ){ if(window[&quot;gf_submitting_5&quot;]){return false;} window[&quot;gf_submitting_5&quot;]=true;  jQuery(&quot;#gform_5&quot;).trigger(&quot;submit&quot;,[true]); }"> <input type="submit" id="gform_submit_button_5" class="gform_button button" value="Submit" onclick="if(window[&quot;gf_submitting_5&quot;]){return false;}  window[&quot;gf_submitting_5&quot;]=true;  " onkeypress="if( event.keyCode == 13 ){ if(window[&quot;gf_submitting_5&quot;]){return false;} window[&quot;gf_submitting_5&quot;]=true;  jQuery(&quot;#gform_5&quot;).trigger(&quot;submit&quot;,[true]); }"> 
            <input type="hidden" class="gform_hidden" name="is_submit_5" value="1">
            <input type="hidden" class="gform_hidden" name="gform_submit" value="5">
            
            <input type="hidden" class="gform_hidden" name="gform_unique_id" value="">
            <input type="hidden" class="gform_hidden" name="state_5" value="WyJbXSIsIjUwOTJlODY2NjY4N2FhMzg3OThkODZhNDZmOThlNGM3Il0=">
            <input type="hidden" class="gform_hidden" name="gform_target_page_number_5" id="gform_target_page_number_5" value="2">
            <input type="hidden" class="gform_hidden" name="gform_source_page_number_5" id="gform_source_page_number_5" value="1">
            <input type="hidden" name="gform_field_values" value="">
            
        </div>
                        </div></div>
                        </form>

这也不是:

$(function(){
    jQuery('#input_1 input:radio').change(function() {
        jQuery('#gform_next_button_5_2').trigger('click');
    });
});

0 个答案:

没有答案