简体   繁体   English

将值从一个javascript函数转换为另一个

[英]Use values from one javascript function into another

I need a little hand here, both this javascript functions are working properly, but I need a value from the first script, so I can "POST" it, but I can't figure out how to make the (string[3]) value to show on the second script and therefore use it. 我需要一点帮助,这两个javascript函数均正常运行,但是我需要第一个脚本中的值,因此我可以对其进行“发布”,但是我不知道如何制作(string [3])要显示在第二个脚本上的值,因此可以使用它。

The only function of this first script is to get values from the getsong.php file 第一个脚本的唯一功能是从getsong.php文件中获取值

<script type="text/javascript">
var string[3]{
$(document).ready(function(){
$("#jquery_jplayer_1").jPlayer({
    ready: function () {
        var data = $.ajax({
          url: "getsong.php",
          async: false
         }).responseText;

         string = data.split('|');
        $(this).jPlayer("setMedia", {
            mp3: string[0]
        }).jPlayer("play");

        $('ol#one').html(string[1]);
        $('ol#two').html(string[2]);
        $('ol#three').html(string[3]);

    },
    ended: function (event) {  
        var data = $.ajax({
          url: "getsong.php",
          async: false
         }).responseText;

         string = data.split('|');
        $(this).jPlayer("setMedia", {
            mp3: string[0]
        }).jPlayer("play");


        $('ol#one').html(string[1]);
        $('ol#two').html(string[2]);
        $('ol#three').html(string[3]);

    },
    swfPath: "js",
    supplied: "mp3"

});
});

Then the only function is second script to post to the "update_db.php" with a value from (string[3]) from the script above, and also is reloading the page at the same time. 然后,唯一的功能是第二个脚本,该脚本将使用上面脚本中的(string [3])值将其发布到“ update_db.php”,并且还将同时重新加载页面。 I need the value in the 'STRING[3] GOES HERE' area. 我需要“ STRING [3]转到此处”区域中的值。

$(function() {
$("#submit01").click(function() {
var song_id = $("#song_id").val();
var dataString = 'song_id='+ 'STRING[3] GOES HERE';

if(song_id=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "update_db.php",
data: dataString,
success: function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);","0");
}
});
}
return false;
});
});


/////
}

</script>

Place var string above the line: var string放在行上方:

$("#jquery_jplayer_1").jPlayer({

And remove var from 并从中删除var

 var string = data.split('|');

You can now access string in any function you want. 现在,您可以在任何所需的函数中访问string

You can make it a global variable by adding something like the following at the top of your page: 您可以通过在页面顶部添加以下内容来使其成为全局变量:

<script>
    var str3 = '';
</script>

Set the value of str3 in your first function and then access it in the latter. 在第一个函数中设置str3的值,然后在后一个函数中访问它。

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

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