1

我在另一种形式上有以下 if/else 并且效果很好。我现在把它放在一个显示为 jquery 对话框的表单上。一路上的每个警报都显示了正确的分配,但是当对话框打开时,两个按钮都没有被选中。

        $("#create-company").click(function() {
            alert($('#primary_company').val().length);
            if ($('#primary_company').val().length > 0) {
                alert("if secondary");
                $('#secondary').attr('checked', 'true');
                var id = $("input:radio[name='companyType']:checked").attr('id');
                alert(id);
            }
            else {
                alert("else primary");
                $('#primary').attr('checked', 'true');
                $('#sec').hide();
                var id = $("input:radio[name='companyType']:checked").attr('id');
                alert(id);
            }
            var id = $("input:radio[name='companyType']:checked").attr('id');
            alert(id);
            $('#popupCreateCompany').dialog('open');
        });

对话:

        $('#popupCreateCompany').dialog(
        {
            autoOpen: false,
            modal: true,
            buttons:
            {
                'Add': function() {
                    var dialog = $(this);
                    var form = dialog.find('input:text, select');
                    $.post('/company/post', $(form).serialize(), function(data) {
                        if (data.Result == "success") {
                            var id = $("input:radio[name='companyType']:checked").attr('id');
                            if (id == "primary") {
                                $('#company').append($('<option></option>').val(data.company_id).html(data.company_name).attr("selected", true));
                                $('#primary_company').append($('<option></option>').val(data.company_id).html(data.company_name).attr("selected", true));
                                $('#company_id').append($('<option></option>').val(data.company_id).html(data.company_name).attr("selected", true));
                            }
                            else {
                                $('#company_id').append($('<option></option>').val(data.company_id).html(data.company_name));
                            }
                            dialog.dialog('close');
                            alert("Company " + data.company_name + " successfully added.");

                        }
                        else {
                            alert(data.Result);
                        };
                    }, "json")
                },
                'Cancel': function() {
                    $(this).dialog('close');
                }
            }
        });

单选按钮:

<label>Company Type:</label>
        <label for="primary"><input onclick="javascript: $('#sec').hide('slow');$('#primary_company').find('option:first').attr('selected','selected');" type="radio" name="companyType" id="primary" />Primary</label>
        <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" name="companyType" id="secondary" />Subsidiary</label>
        <div id="sec">
            <fieldset>
            <label for="primary_company">Primary Company:</label>
            <%= Html.DropDownList("primary_company", Model.SelectPrimaryCompanies, "** Select Primary Company **") %>
            </fieldset>
        </div>
4

1 回答 1

2

加载对话框时 DOM 已更改,因此您需要使用回调方法重新初始化表单值。

尝试对话框的“打开”或“焦点”回调事件:

$('#popupCreateCompany').dialog({
      open: function(event, ui) { ... }
});
于 2010-04-26T13:32:17.320 回答