从jquery移动对话框窗口执行父函数

时间:2014-09-22 16:51:09

标签: javascript jquery-mobile

无论如何都要执行父函数并从jquery移动对话框窗口中影响父变量吗?

[已编辑添加代码]

在父级上,用户点击对话框的链接:

<a href="options-window.php" data-rel="dialog" id="dlg-options-window"></a>

然后进入类似的对话框(sliderOneVal和sliderTwoVal是由父项创建并填充的变量):

<div id="option-window" data-role="page">
<script type="text/javascript">

$('#layer-window').on('dialogcreate', function( event, ui ){
    $('#sliderOne').val(sliderOneVal);
    $('#sliderTwoOption').val(sliderTwoVal);    

    $('.sliderSelector').bind('change', sliderClicked() );    

});
</script>
        <div data-role="header" data-theme="d"><h5>Options</h5></div>
        <div data-role="content">
            <fieldset data-role="controlgroup" data-type="vertical" data-role="fieldcontain"><table>
            <tr>
                <td><select name="sliderOne" id="sliderOne" class="sliderSelector" data-theme="" data-role="slider" data-mini="true">
                    <option value="off">Off</option>
                    <option value="on">On</option>
                </select></td>
                <td><label for="sliderOne">Option One</label></td>
            </tr>
            <tr>
                <td><select name="sliderTwo" id="sliderTwo" class="sliderSelector" data-theme="" data-role="slider" data-mini="true">
                    <option value="off">Off</option>
                    <option value="on">On</option>
                </select></td>
                <td><label for="sliderTwo">Option Two</label></td>
            </tr>
            </table></fieldset>
        </div>
</div>

这里是sliderClicked(),它在父文档中,它获取了哪些滑块及其当前值,然后相应地更改了相应变量的值:

function sliderClicked(){
    sliderVal = $(this).val();
    sliderID = $(this).attr("id");


    if(sliderVal=="on" && sliderID=="sliderOne"){
        sliderOneVal="on";
    } else if(sliderVal=="off" && sliderID=="sliderOne"){
        sliderOneVal="off";
    }

    if(sliderVal=="on" && sliderID=="sliderTwo") {
        sliderTwoVal="on";
    } else if(sliderVal=="off" && sliderID=="sliderTwo"){
        sliderTwoVal="off";
    }
    console.log('sliderOneVal:'+sliderOneVal);
    console.log('sliderTwoVal:'+sliderTwoVal);
}

1 个答案:

答案 0 :(得分:0)

我需要将函数sliderClicked移动到与对话框页面相同的文件,并将其中的javascript更改为:

$(document).delegate('#option-window', 'pageinit', function() {
    $('#sliderOne').val(sliderOneVal).slider("refresh");
    $('#sliderTwoOption').val(sliderTwoVal).slider("refresh");

    $('.sliderSelector').bind('change', sliderClicked);
});