2013-10-16 18 views
0

我试图使用传递我的car值到另一个函数,我不知道如何。我试图把整个功能btn-info-add放到.span8。但是它会在第二次执行两次。如何使用modal.show传递值

$(".span8").on("click", "table #trID", function() { 
    var car = ($(this).closest("tr").children("td").eq(1).html()); 
    $('#myModal1').modal('show'); 
}); 

$("#btn-info-add").click(function() //button inside the modal 
    selectCourse(car); //execute ajax 
}); 

回答

0
var car; //car must be declared out of your function to be available for both functions 
$(".span8").on("click", "table #trID", function() { 
car = ($(this).closest("tr").children("td").eq(1).html()); 
$('#myModal1').modal('show'); 
}); 
$("#btn-info-add").click(function() //button inside the modal 
selectCourse(car); //execute ajax 
}); 
+0

是的,我知道这种方法,但我有10功能,我吓倒它可能有错误。 – user2863217

+0

我的意思是如果我使用相同的变量为所有的功能。我已经做了,但我仍然认为我应该遵循这种方法,谢谢! – user2863217

0

您可以在对话框中创建一个隐藏的元素(input将是巨大的),并为其分配你的欲望值。

<div id="dialog" title="Basic dialog"> 
    <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p> 
    <input id="carvalue" type="hidden" value=""/> 
</div> 

注意,我创建了一个input元素(隐藏,当然)这是要保存,我想以后访问值。在此之后,你可以修改你这样的代码:

$(".span8").on("click", "table #trID", function() { 
    var car = ($(this).closest("tr").children("td").eq(1).html()); 
    $("#carvalue").val(car); // Store value into hidden input 
    $('#myModal1').modal('show'); 
}); 

$("#btn-info-add").click(function() //button inside the modal 
    var car = $("#carvalue").val(); // retrieve value from input hidden 
    if(car != ""){ 
     selectCourse(car); 
    } 
}); 

这种技术在形式通常用来传递AJAX调用的附加信息。你的用户不会注意到它的存在,你可以继续工作。快乐的编码!

编辑:

JQuery的有一个称为jQuery.data到信息存储到JQuery的元件的方法。所以你的值将被存储在元素本身。您的代码将如下所示:

$(".span8").on("click", "table #trID", function() { 
    var car = ($(this).closest("tr").children("td").eq(1).html()); 
    jQuery.data("#btn-info-add", "car", car); // store data inside jQuery element 
    $('#myModal1').modal('show'); 
}); 

$("#btn-info-add").click(function() //button inside the modal 
    selectCourse(jQuery.data("#btn-info-add", "car")); //execute ajax 
}); 

我希望它可以帮助您。快乐的编码!

+0

我想使用.text,但用户可能能够更改html数据。所以我无法用于我的学校工作。 – user2863217

+0

@ user2863217我编辑我自己的答案与另一个解决方案:) –

+0

OMG这是非常有用的!我会再次更改我的代码... – user2863217