0

我试图将我的car值传递给另一个函数,我不知道如何。我试图将整个功能btn-info-add放入.span8. 但这将在第二次执行两次。

$(".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
});
4

2 回答 2

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
});
于 2013-10-16T17:32:38.917 回答
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
});

我希望它对你有帮助。快乐编码!

于 2013-10-16T18:40:19.203 回答