2016-02-25 40 views
1

我使用重力形成WordPress插件,我想oninput添加到我的表單標籤... 我下面這個網頁上的說明: https://www.gravityhelp.com/documentation/article/gform_form_tag/如何使用此示例將代碼添加到JavaScript表單標記中?

然而,他們所提供的例子是用於替換的東西在表單標記中,而不是將某些內容添加到表單標記....應該如何編輯以下代碼,以將oninput事件添加到標記,而不是替換標記內部的某些內容?我不知道,而不是使用pregreplace什麼...

add_filter('gform_form_tag', 'form_tag', 10, 2); 
function form_tag($form_tag, $form) { 
    if ($form['id'] != 3) { 
     //not the form whose tag you want to change, return the unchanged tag 
     return $form_tag; 
    } 
    $form_tag = preg_replace("|action='(.*?)'|", "action='custom_handler.php'", $form_tag); 
    return $form_tag; 
} 

回答

1

好了,我們這裏有XY Problem的經典案例。您在問如何更改標籤以包含oninput屬性,但是您確實在嘗試完成其他功能。

所以,首先我會直接回答你的問題:

add_filter('gform_form_tag', 'form_tag', 10, 2); 
function form_tag($form_tag, $form) { 
    if ($form['id'] != 3) { 
     //not the form whose tag you want to change, return the unchanged tag 
     return $form_tag; 
    } 
    // We know the form has the 'form ' in it, so replace that with 'form' plus the oninput you want 
    $form_tag = str_ireplace("<form ", "<form oninput='myFunction() ", $form_tag); 
    return $form_tag; 
} 

不過說真的,有可能是一個更好/更簡單的方法。

寫一些JavaScript - 因爲你清楚要使用的JavaScript - 與形式連接:

// Since WordPress loads jQuery in no-conflict mode, this is the preferred "document ready" 
jQuery(function($) { 
    // "gform_3" represents the form you want to target. 
    $('#gform_3 input, #gform_3 textarea').on('change', function() { 
     // Do your "oninput" work here 
    } 
}); 

只需更改ID來匹配你要定位的形式的ID - 如果你的例如,表單是ID 23,然後使其成爲​​。

+0

謝謝你這麼多的詳細答覆。真的很感激它。你的第一個解決方案是完美的,但是,我不確定最後一個......如何開始我的輸入代碼....我會litterally只是取代「/ /你的'oninput'在這裏工作」與代碼這意味着在ininput屬性的雙引號內?所以我應該只添加例如:「amount.value = rangeInput.value;」還是我必須開始與其他事情? – Shtarley

0

我同意Cale的簡單答案;但是,在某些情況下,您仍然可能要將事件屬性(任何事件屬性)放在<表單上。這是一個非常簡單的插件。

http://gravitywiz.com/gravity-forms-tag-editor/

用法示例:

new GW_Tag_Editor(array(
    'tag'   => 'form', 
    'form_id'  => 123, 
    'oninput'  => 'myFunction()' 
)); 
相關問題