开发者

Click button alert message in wordpress by Jquery

i want to click button save alert message in word press plugin by using jquery

PHP CODE:

function ss_load_script() {
    echo "<script type='text/javascript' src='".get_bloginfo('wpurl') ."/wp-content/plugins/Test/msg.js'></script>" ."\n";
}

require_once("view.php");
//add_action('wp_head', 'ss_load_script');
add_filter('the_content', 'test');
add_action('wp_footer','display_copyright');

View.php

function test($content) {
        ss_load_script();
        if(strpos($con开发者_运维技巧tent, '[wel]')){

                 createform();
                 }
    return $content;
}
function createform(){?>
        <form id="frmtest1">
                <table style="width:100%;border:1px;">
                    <tr>
                        <td colspan="2" align="center">Register</td>
                    </tr>
                    <tr>
                        <td align="right">UserName</td><td align="left"><input type="text"  id="txtusername"/></td>
                    </tr>
                    <tr>
                        <td align="right">Password</td><td align="left"><input type="text"  id="txtpassword"/></td>
                    </tr>
                    <tr>
                        <td></td><td align="left"><input type="button" id="btnsave" name="btnsave" onclick="a()" value="Save" /></td>
                    </tr>
                </table>
        </form>

Javascript:

function a(){
    $.noConflict();
    jQuery(document).ready( function () {                              
        $("#btnsave").click(function(){
                    alert("test")
                });
    });
}


First you should load the jQuery scripts provided in the WordPress install. You do this in the main plugin call, something like this:

myplugin.php

...
add_action('wp_enqueue_scripts', 'setup_scripts'); 

function setup_scripts() {
     wp_enqueue_script('jquery');  
}

Then in your view you can output the custom script call.

view.php

...
<?php
  // do stuff
?>
<script language="javascript" type="text/javascript">
jQuery('#btnsave').click(function() {
    alert('Hello');
});
</script>

Depending on when your script is called & when the DOM is finished rendering you may need to wrap the call in a document ready call like this:

view.php

...
<?php
  // do stuff
?>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#btnsave').click(function() {
        alert('Hello');
    });
});
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜