将Jquery变量传递给HTML,以便可以在多个函数中获取它

时间:2014-07-08 14:22:14

标签: javascript jquery html jquery-mobile

我正在努力让这个工作,但我可能会完全偏离这个。我正在创建一个jquery移动webapp,我正在尝试使用一个在创建弹出窗口时声明的变量,在不同的点击功能上。我认为这样做的方式是:

点击此处运行函数LoadCancelReservationPopup并将一些id传递给JQ

<a onclick= LoadCancelReservationPopup('" . $row ['sizecodeid'] ."','" . $row ['unitid'] ."') class='ui-btn ui-mini'>Cancel</a>

运行此功能

function LoadCancelReservationPopup(sizecode,unit) {

var sizecode = (sizecode);
var unit = (unit);

$("#cancelReservationSizecode").html(sizecode);
$("#cancelReservationUnit").html(unit);

 $("#reservationCancelPopup").popup("open");
$("#reservationCancelPopup").trigger('create');

};

该函数打开此弹出窗口,传递的ID现在显示在div中

<div data-role='popup' id='reservationCancelPopup' data-theme='a' style='min-width: 300px;'>
    <div data-role='main' class='ui-content'>
        <div data-role='header' data-theme='a'>
            <h1>Cancel Reservation</h1>
        </div>
        <p>Are you sure you want to cancel this reservation?</p>
        <div id='cancelReservationUnit' name='cancelReservationUnit' class="unit"></div>
        <div id='cancelReservationSizecode' name='cancelReservationSizecode' class="sizecode"></div>

        <a href='#' class='ui-btn ui-btn-c' data-transition='pop' id='loadCancelCustomerReservation' name='loadCancelCustomerReservation'>Yes Cancel Reservation</a>
        <a href='#' class='ui-btn ui-btn-c' data-rel='back' data-transition='pop' id='CloseCancelCustomerReservationPopup'>No Return to Customer Details</a>

    </div>
</div>

我遇到的问题是,当调用loadCancelCustomerReservation函数时,我希望包含jquery变量的弹出窗口是空的,这些变量最终将通过ajax传递给PHP,但我不能弄清楚如何在我的2个不同功能中使用这些变量。是不可能以这种方式传递jquery变量?

$("#loadCancelCustomerReservation").click(function(){

  var sizecode = $('#cancelReservationSizecode').val();
  var unit = $('#cancelReservationUnit').val();

    window.alert(sizecode);
    window.alert(unit);
});

1 个答案:

答案 0 :(得分:2)

当您使用.val()设置值时,您在<div>代码上呼叫.html()

将您的代码更改为:

$("#loadCancelCustomerReservation").click(function(){

  var sizecode = $('#cancelReservationSizecode').html();
  var unit = $('#cancelReservationUnit').html();

    window.alert(sizecode);
    window.alert(unit);
});