简体   繁体   English

从PHP执行jQuery代码

[英]executing jquery code from php

$("#bt-potrdi").click( function(e) {    
        e.stopPropagation();        
        $("#belina").css({"z-index":200}); $("body").addClass("ext");
        $("#vpisok_frame").css({"z-index":250}).fadeIn(200);        
    });

when i click on button this jquery code is executed and works fine. 当我单击按钮时,此jQuery代码已执行且工作正常。 Can i execute this code without click event? 我可以在没有点击事件的情况下执行此代码吗?

I want to execute this code from php when some data is executed successfully like for example 当某些数据成功执行时,我想从php执行此代码,例如

if ($ok) {
?>
//execude ajax code
e.stopPropagation();        
            $("#belina").css({"z-index":200}); $("body").addClass("ext");
            $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
<?php
}

is this possible? 这可能吗? PHP is server side code so i didn't find any good example if this is possible PHP是服务器端代码,因此如果可能,我没有找到任何好的示例

Similar to the other suggestions, but this doesn't rely on having 2 copies of the same code... 与其他建议类似,但这不依赖于拥有两个相同代码的副本...

if ($ok) {
?>
<script type="text/javascript">
    $(function() {
        $("#bt-potrdi").trigger("click");
    });
</script>
<?php

If you ever change the click event handler, this will still work. 如果您更改了click事件处理程序,则该操作仍然有效。 This means that you won't need to make any future changes in 2 places. 这意味着您以后无需在2个地方进行任何更改。

If I understand the question, you want to execute some javascript on page load if $ok is true. 如果我理解这个问题,如果$ok为true,则希望在页面加载时执行一些javascript。 To do that, you should be able to do something like: 为此,您应该可以执行以下操作:

if ($ok) {
?>
<script type="text/javascript">
   //execute ajax code
   $("#belina").css({"z-index":200}); $("body").addClass("ext");
   $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
</script>
<?php
}

EDIT: Also, e.stopPropagation(); 编辑:另外, e.stopPropagation(); is not going to work because e is not defined anywhere. 因为e在任何地方都没有定义,所以无法使用。

I would design my code so that i could add classes and set z-indexes straight up when html is rendered, but if you want to do those with jquery, jsut wrap them in <script> and $(document).ready(function({}); so they will be executed when dom is ready. 我将设计代码,以便在呈现html时可以添加类并直接设置z-indexes,但是如果要使用jquery进行处理,则jsut将它们包装在<script>$(document).ready(function({});因此将在dom准备就绪时执行它们。

eg. 例如。

if ($ok) {
?>
//execude ajax code
<script>
$(document).ready(function{

    $("#belina").css({"z-index":200}); $("body").addClass("ext");
    $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
});
</Script>
<?php
}

edit Okay i assumed e.stopPropagation(); 编辑好吧,我假设e.stopPropagation(); is set somewhere before since it was in questions example aswell. 设置在之前的某个位置,因为它也在示例中。 removed it for now. 现在将其删除。

What happens is when your PHP script is executed, if $ok evaluates to true, then your jquery code is included in the generated document, and is omitted if it doesn't. 发生的情况是在执行PHP脚本时,如果$ ok计算为true,则您的jquery代码将包含在生成的文档中,如果没有,则将其省略。 But at this point, there is no event, so the following line will not work. 但是,目前尚无任何事件,因此以下行将不起作用。

e.stopPropagation();

However, as jdwire suggested, you can wrap your javascript in a script tag, and have it executed that way, without being triggered by an event. 但是,正如jdwire所建议的那样,您可以将javascript包装在script标签中,并以这种方式执行它,而不会被事件触发。 So... like this: 所以...像这样:

<?php
if ($ok) {
?>
<script type="text/javascript">
    $("#belina").css({"z-index":200}); $("body").addClass("ext");
    $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
</script>
<?php
}
?>

You can echo out the code in <script/> tags and it will be run as JavaScript, or with the code you have, just place it between tags and it should be fine :) 您可以echo了在代码<script/>标签,它会运行如JavaScript,或者用你的代码,只需将它标记之间,它应该罚款:)

But you cannot do this "live". 但是您不能“现场”进行此操作。 Only when the page is requested ONCE. 仅在一次请求页面时。

You can also do it this way to pass PHP variables over to javaScript. 您也可以通过这种方式将PHP变量传递给javaScript。 I think it's a lot cleaner. 我认为这要干净得多。

$check = 1;

?>

...

<script type="text/javascript">

    var check = <?= $check; ?>;

    ...

    if (check)
    {
        $("#bt-potrdi").trigger("click");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM