JQuery的对话框内访问变量(Access variables inside JQuery dia

2019-10-19 13:33发布

我有一个用户ID在viewBag我要访问的ViewBag在我的对话框

<script>
    $(function () {
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
            width: 339,
            overlay: {
                backgroundColor: 'red',
                opacity: 0.5
            },
            show: {
                effect: "blind",
                duration: 500
            },
            hide: {
                effect: "explode",
                duration: 700
            }
        });
    });

    function OpenDialog()
    {
        $('#dialog').dialog("open");
    }
</script>

<div id="open" onclick="OpenDialog();">Click To Open</div>  

  @ViewBag.UserID
    <div id="dialog">   
            <table>
                <tr>
                    <td>User ID</td>
                    <td>@ViewBag.UserID</td>
                </tr>
                <tr>
                    <td>Comments</td>
                    <td>@Html.TextArea("CommentsTextBox")</td>
                </tr>
            </table>
            <div class="alignCenter"> 
                 <input type="submit" value="Add" /> 
                 <input type="button" value="Cancel" /> 
            </div>
    </div>

Answer 1:

您的代码应该有工作,但可能是jQuery的对话框的div改变DOM元素值<div id="dialog">

你可以试试下面的方法。

请用户ID的隐藏字段。

<input type="hidden" id="userId" value="@ViewBag.UserID" />

让里面的用户ID占位符<div id="dialog">

<div id="dialog">
    <table>
        <tr>
            <td>User ID</td>    
            <td><span id="dialogUserId"></span></td>
        </tr>
    </table>
</div>

获取用户名和上OpenDialog()事件分配形式占位符。

<script>
function OpenDialog()
    {
        $("#dialogUserId").val($("#userId").val()); //if this won't work try adding this line after dialog("open")
        $('#dialog').dialog("open");
    }
</script>


Answer 2:

如果你只是想访问内部jQuery的模型变量或ViewBag,你可以试试下面的方法。

<script type="text/javascript">

    function set(value) {
        return value;
    }

    alert(set(@Html.Raw(Json.Encode(Model.Message)))); // Message set from controller
    alert(set(@Html.Raw(Json.Encode(ViewBag.UrMessage))));

</script>

谢谢



Answer 3:

做一件事把你的用户ID普通的Java脚本变量和文件准备得用j查询位值到



文章来源: Access variables inside JQuery dialog