简体   繁体   English

如何在php文件中使用jquery代码?

[英]How can i use jquery code in php file?

I need to add a property to the input[name=$key] after a control inside a PHP file 我需要在PHP文件中的控件后向input[name=$key]添加一个属性

Code: 码:

if ( !isset( $field_obj[ $key ] ) ) 
{
?>
    <script> $('input[name=$key]').prop("data-toggle", "popover");</script>
<?php

but $key is declared in PHP section and i can't use it inside the script section. 但是$key是在PHP部分声明的,我不能在script部分内部使用它。 So how can I do it? 那我该怎么办呢?

<script> $('input[name=<?php echo $key ?>]').prop("data-toggle", "popover"); </script>

但这是一个坏习惯。

Not recommended but try this, 不推荐,但是尝试一下,

<?php    
if ( !isset( $field_obj[ $key ] ) ) {
?>
<script> $('input[name=<?php echo $key ?> ]').prop("data-toggle", "popover"); </script>
<?php
}
?>

The bad practice that everyone keeps going on about is the practice of generating your Javascript code. 每个人都在进行的不良做法是生成Javascript代码的做法。

Best practice would be to have all of your JS code as static as possible, loaded in a separate .js file. 最佳做法是将所有JS代码尽可能地静态,并加载到单独的.js文件中。

In order to get this bit of code to trigger when the PHP variable is set, you would do something like this: 为了使这段代码在设置PHP变量时触发,您可以执行以下操作:

In your main JS code file, you would have a block like this which runs when the page is loaded: 在您的主要JS代码文件中,您将有一个类似这样的块,该块在页面加载时运行:

for(var key in window.pageConfig.keys) {
    $('input[name='+key+']').prop("data-toggle", "popover");
}

This code would be loaded exactly the same every time the page loads. 每次页面加载时,该代码将完全相同地加载。 However, your PHP code would generate a small chunk of JS code that populates the window.pageConfig object with the relevant data. 但是,您的PHP代码将生成一小段JS代码,并使用相关数据填充window.pageConfig对象。

Your PHP code might look something like this: 您的PHP代码可能看起来像这样:

$jsonKeys = json_encode(['keys'=>array_keys($field_obj)]);
print "<script>var pageConfig = {$jsonKeys};</script>";

This would produce the pageConfig object in Javascript which the rest of your code could then use to work out which keys to process. 这将在Javascript中生成pageConfig对象,其余的代码可用于确定要处理的键。

Note, I've made some assumptions in this PHP code; 注意,我在此PHP代码中做了一些假设; eg I'm assuming you want to run the jQuery code for every field_obj . 例如,我假设您想为每个field_obj运行jQuery代码。 If that assumption is wrong, then feel free to change the code to suit, but hopefully the example I've given you will take you along the right path. 如果这个假设是错误的,那么可以随意更改代码以适合自己,但是希望我给出的示例将带您正确的道路。

They key point here is to minimise the amount of JS code that your PHP code generates; 它们的重点是最大程度地减少PHP代码生成的JS代码的数量。 certainly avoid generating anything other than JS data objects in PHP; 当然要避免在PHP中生成除JS数据对象以外的任何东西; the rest of your JS code should be static. 剩下的JS代码应该是静态的。

Try to echo $key : 尝试回显$key

<?php
if ( !isset( $field_obj[ $key ] ) ) {
    ?>
    <script>
        $('input[name=<?= $key ?>').prop("data-toggle", "popover");
    </script>
    <?php
}
?>
<?php    
if ( !isset( $field_obj[ $key ] ) ) {
?>

<script>
    $('input[name="<?php echo $key ?>" ]').prop("data-toggle", "popover"); 
</script>

<?php
}
?>

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

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