修改jquery-modal事件中的变量值

时间:2015-04-14 16:50:25

标签: javascript jquery modal-dialog

我试图在BEFORE_CLOSE事件中修改变量的值,到目前为止没有运气......我正在使用Kylefox's Jquery-modal

HTML:

<div id="other" class="modal" style="display:none;">
<label for="form_otherName" class="label-important required">(*) Enter 'Other' Name </label>
<input type="text" required="required" name="form[otherName]" class="round default-width-input" id="simple-input">
<a href="#close" rel="modal:close">Close window</a></div>

JS:

      $('#form_submit').on('click', function (e) {
        e.preventDefault();
        var page = $("#carousel").rcarousel("getCurrentPage");
        var employeeID = $("input[name='form[employeeID]']").val();
        var temp = '';
       if (page == 4) {
            $('#other').modal();
            $('#other').on($.modal.BEFORE_CLOSE, function (event, modal) {
                temp = $("input[name='form[otherName]']").val();
                console.log(temp);
            });
            console.log('after event '+temp);
            $.post("{{ path("working_letter_model",{'language': 'spanish'})}}", {model: page, idNumber: employeeID}, function (data) {
                //do whatever with the response

                });
            }
}

我做错了什么?无论如何要解决这个问题吗?

我读到Js使用变量作为值而不是参考,但我不知道这是否适用。

1 个答案:

答案 0 :(得分:1)

您分配临时变量的方式没有任何问题,只是以您记录它的方式。好像你正在阅读这段代码并期望它以线性方式执行,即按照它出现的顺序执行。这是实际发生的事情。

事件在此处注册,并提供回调以在事件触发时执行。

$('#other').on($.modal.BEFORE_CLOSE, function (event, modal) {
    //this code does not execute yet!
    temp = $("input[name='form[otherName]']").val();
    console.log(temp); //it shows the value
});

接下来,此行执行且temp仍具有其初始值。

console.log('after event '+temp); //it returns empty

最后,当模态关闭时,将执行回调并从内部获取控制台日志,该日志显示新分配的值。

temp = $("input[name='form[otherName]']").val();
console.log(temp); //it shows the value