从php页面调用jquery函数 [英] Call jquery function from php page

查看:109
本文介绍了从php页面调用jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php页面中包含的js文件中创建了一个jQuery函数.

I created a jquery function in a js file that is included in a php page.

js文件

$( function() {

    function disab(){
        $('input#idname').attr('disabled', 'disabled');
    }

});

在我的php文件中,我试图以这种方式调用disab函数,但没有成功.

In my php file I tried to call the disab function in this way but with no success.

echo "<script>";
echo "$(function(){";
echo "disab();";
echo "});";
echo "</script>";

我该怎么做?谢谢

推荐答案

首先将您的函数放在js文件中的document.ready函数之外...以便它是全局的,可以从任何地方访问. >

first of all put your funciton outside the document.ready function in your js file...so that it is global and can be accessed from anywhere.

 function disab(){
    $('input#idname').attr('disabled', 'disabled');
 }

 $( function() { .. }); //use if needed..

并调用php文件中<script>标记内的函数

and call the function inside <script> tag in php file

 <?php
 //all you php related codes
 ...
 ?>
 <script>
    $(function(){
        disab();
    });
 </script>

最重要的是,如果使用的是最新版本的jquery(jquery 1.6+),请使用prop()而不是attr()

and most important, use prop() instead of attr() ... if you are using latest version of jquery (jquery 1.6+ )

  $('input#idname').prop('disabled', true);

这篇关于从php页面调用jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆