【问题标题】:How can i pass a variable with url in javascript code?如何在 javascript 代码中传递带有 url 的变量?
【发布时间】:2017-03-02 06:36:59
【问题描述】:

我想在 JavaScript 代码中传递一个带有 URL 的变量。变量来自数据库。我在 StackOverflow 中看到了很多答案。但我认为,我没有为我的问题找到任何正确的答案。代码如下:

此代码在我的索引页中。

 <script type="text/javascript">

          $(document).ready(function(){

            $("#btn-view").hide();

            $("#btn-add").click(function(){
                $(".content-loader").fadeOut('slow', function()
                {
                    $(".content-loader").fadeIn('slow');
                    **$(".content-loader").load('video_add.php?lang=$lang')**;
                    $("#btn-add").hide();
                    $("#btn-view").show();
                });
            });

            $("#btn-view").click(function(){

                $("body").fadeOut('slow', function()
                {
                    **$("body").load('video.php?lang=$lang');**
                    $("body").fadeIn('slow');
                    **window.location.href="video.php?lang=$lang";**
                });
            });

        });
    </script>

此代码在 code.js 文件中。

// JavaScript Document

$(document).ready(function(){

/* Data Insert Starts Here */
$(document).on('submit', '#emp-SaveForm', function() {

   $.post("video_create.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-SaveForm")[0].reset();
         });    
     });   
     return false;
});
/* Data Insert Ends Here */


/* Data Delete Starts Here */
$(".delete-link").click(function()
{
    var id = $(this).attr("id");
    var del_id = id;
    var parent = $(this).parent("td").parent("tr");
    if(confirm('Sure to Delete ID no = ' +del_id))
    {
        $.post('video_delete.php?lang=$lang', {'del_id':del_id}, function(data)
        {
            parent.fadeOut('slow');
        }); 
    }
    return false;
});
/* Data Delete Ends Here */

/* Get Edit ID  */
$(".edit-link").click(function()
{
    var id = $(this).attr("id");
    var edit_id = id;
    if(confirm('Sure to Edit ID no = ' +edit_id))
    {
        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('video_edit.php?edit_id='+edit_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });
    }
    return false;
});
/* Get Edit ID  */

/* Update Record  */
$(document).on('submit', '#emp-UpdateForm', function() {

   $.post("video_update.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-UpdateForm")[0].reset();
             $("body").fadeOut('slow', function()
             {
                $("body").fadeOut('slow');
                window.location.href="video.php?lang=$lang";
             });                 
         });    
    });   
    return false;
});
/* Update Record  */
});

所有 URL 都传递一个变量

lang=$lang

。 $lang 是 en 或 bn 但它来自数据库表。我怎样才能使它可用?

【问题讨论】:

  • 在javascript代码中使用php,你是什么
  • 在需要使用的js中回显php

标签: javascript php jquery variables url


【解决方案1】:

您可以像这样在索引页面上设置 javascript 数组。

注意:将此脚本放在包含您的 code.js 文件之前

<script>
var global_var = {
lang: "your value",
};
</script>

现在在您的 code.js 文件中

'video_delete.php?lang='+global_var.lang

【讨论】:

    【解决方案2】:

    如果您想将数据传递给 url,并且您没有使用内联 JS,那么您可以声明全局变量,然后在 *.js 文件中使用它。

    例如: 此代码在我的索引页中。

    <script type="text/javascript">
    // Decl Global variable 
    var $lang = "<?php print $lang; ?>";
    
    // HEre your normal js code
    </script>
    

    此代码在 code.js 文件中。

     // js code 
     $.post("video_update.php?lang="+$lang, $(this).serialize())
     // Js code 
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多