我在其他表单上有以下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>
答案 0 :(得分:1)
DOM在加载对话框时已更改,因此您需要使用回调方法重新初始化表单值。
尝试对话框的“打开”或“焦点”回调事件:
$('#popupCreateCompany').dialog({
open: function(event, ui) { ... }
});