你可以在jquery模式对话框中提交表单吗?

Jam*_*mie 9 django jquery jquery-ui

我在jquery ui模式对话框中有一个表单,当我单击提交按钮时没有任何反应.有没有办法在jquery模式对话框中提交表单而无需在javascript中编码按钮?

这是我的代码片段;

    <script>
    // increase the default animation speed to exaggerate the effect

    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
                    modal: true,

        });

        $( "#opener" ).click(function() {
            $( "#dialog" ).dialog( "open" );
            return false;
        });
    });

    function SetValues()
    {
        var s = 'X=' + window.event.clientX +  ' Y=' + window.event.clientY ;
        document.getElementById('divCoord').innerText = s;

    } 
    </script>

<div id="dialog" title="new task">
        <form method="post" action="/projects/{{ project.slug }}/">
            <div class="form-row">
                <label class="required" for="title">task type</label>
                <select name="type">
                {% for TaskType in project.tasktype_set.all %}
                    <option value="{{ TaskType.name }}">{{ TaskType.name }}</option>
                {% endfor %}
                </select>
            </div>
            <div id="buttons">
                <input type="submit" value="create"/>
            </div>
        </form>
</div>

<button id="opener">new task</button>
Run Code Online (Sandbox Code Playgroud)

如果我删除"模态:真",使对话非模态,它将提交.

z.e*_*yyo 6

您可以在对话框中使用$ .post()或$ .ajax(),例如:

$( "#dialog" ).dialog({
    autoOpen: false,
    draggable: false,
    resizable: false,
    modal: true,
    buttons: {
        "Save": function() {
            var type = $("#type option:selected").val();
            $.post("/projects/{{project.slug}}/", {type:type}, function(data) {
                alert(data);// ---> data is what you return from the server
            }, 'html');
         },
         "Close": function(){
            $(this).dialog('close');   
         }

    }
});
Run Code Online (Sandbox Code Playgroud)

只需给你的select标签一个id ......就可以更容易地获取值.也摆脱了输入按钮,因为现在你将从对话框中保存按钮.