简体   繁体   English

在jQuery .val中使用PHP?

[英]Use PHP inside a jQuery .val?

Is it possible to get a value from PHP in jQuery? 是否可以从jQuery中的PHP获取值? I have the following PHP: 我有以下PHP:

$attachment_id = attachment_url_to_postid( $_POST[ 'background-video' ] );
$video_meta = get_post_meta( $attachment_id , '_wp_attachment_metadata', true );

And I want to use the value $video_meta['length'] inside a jQuery that right now looks like this: 我想在现在看起来像这样的jQuery中使用值$video_meta['length']

jQuery('input[name=slide_duration]').val("PHPVIDEOMETALENGTH");

Is this possible with jQuery? jQuery有可能吗? EDIT: This is in a separate .js file which is enqueued. 编辑:这是在一个单独的.js文件中排队。

No, as you have a separate .js file, you can not use PHP in it. 不,因为您有单独的.js文件,所以不能在其中使用PHP。 Because the server will not treat it as a php file or code. 因为服务器不会将其视为php文件或代码。

But you can do this by another way around, 但是您可以通过另一种方式来做到这一点,

put one hidden tag in the same page (which sould be a php file): 在同一个页面上放置一个隐藏标签(该文件应该是一个php文件):

<input type="hidden" id="slide_duration" value="<?php echo $video_meta['PHPVIDEOMETALENGTH'] ?>">

And then using jquery, get its value in JS code. 然后使用jquery在JS代码中获取其值。

var videolength = $('#slide_duration').val();

The only way you can use PHP in a JavaScript file is if you serve that file through PHP . JavaScript文件中使用PHP的唯一方法是通过PHP提供该文件。 For example, your PHP file should be like: 例如,您的PHP文件应类似于:

<?php
   header("Content-Type: application/javascript");
   require "path/file.js";
?>

Then, you can simply write your PHP code inside the js file: 然后,您可以简单地在js文件中编写PHP代码:

jQuery('input[name=slide_duration]').val("<?= $video_meta['length'];?>");

To load the PHP file, you can use a script element just as you would do with a JavaScript file. 要加载PHP文件,可以像使用JavaScript文件一样使用script元素。

assign the value to the js var before the separate.js file 将值分配给separate.js的js变量之前的js var

<script>var meta_length = '<?=$video_meta['length'];?>'</script>
<script src="separate.js"></script>

separate.js file 独立的.js文件

$(function(){    
    jQuery('input[name=slide_duration]').val(meta_length);    
});

you can set $video_meta in a div or span in php file. 您可以在div设置$video_meta或在php文件中span

Eg: 例如:

 <span id="video_meta_vale" style="display:none;"><?php echo $video_meta;?></span>

And then just retrieve it from js using id 然后使用idjs检索它

Eg: 例如:

 var t = $('#video_meta_vale').html();
 jQuery('input[name=slide_duration]').val(t);

Try this, 尝试这个,

First set the value inside the hidden field ,then get in js. 首先在隐藏字段中设置值,然后进入js。

 $video_meta = get_post_meta( $attachment_id , '_wp_attachment_metadata', true );

<input type="hidden" value=<?php echo $video_meta; ?> id="video_meta">

<script>
var video_meta = $('#video_meta').val();
alert(video_meta);
</script>

jQuery in PHP PHP中的jQuery

<?php

    $attachment_id = attachment_url_to_postid( $_POST[ 'background-video' ] );
    $video_meta = get_post_meta( $attachment_id , '_wp_attachment_metadata', true );

    /* echoing <script> */
    echo "
    <script>

        $(function()
        {
            $('input[name=slide_duration]').val({$video_meta['length']});
        });

    </script>";

?>

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

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