![](/img/trans.png)
[英]How can I pass variable from the onclick of a button to the javascript code?
[英]How can i pass a variable with url in javascript code?
我想在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 */
});
所有網址都傳遞了一個變量
lang = $ lang
。 $ lang是en或bn,但它來自數據庫表。 我如何使它可用?
您可以像這樣在索引頁面上設置javascript數組。
注意:請將此腳本放在包含您的code.js文件之前
<script>
var global_var = {
lang: "your value",
};
</script>
現在在您的code.js文件中
'video_delete.php?lang='+global_var.lang
如果要向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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.