对话框中的jquery fire事件

时间:2015-10-19 12:25:44

标签: javascript jquery

如何将选择更改事件保存到不会触发的对话框中?

我的HTML:

    <body>
    <div id="dialog_message"></div>
    </body>

我的JAVASCRIPT:

    var titolo = 'my dialog';
    var s="<select id=\"MYSEL1\" name=\"MYSEL1\"><option value=\"A\">opt A</option><option value=\"B\">opt B</option></select><br><select id=\"MYSEL2\" name=\"MYSEL2\"></select>";

    $( "#dialog_message" ).dialog({
        resize: 'auto',
        draggable: true,
        resizable: false,
        closeOnEscape: false,
        open: function() {      // open event handler
            $(this).parent().find(".ui-dialog-titlebar-close").hide();
            $(this).html(s);
        },
        buttons: {
            Annulla: function() {
                $( this ).dialog( "close" );
            }
        },
        title: titolo
    });
    $("select").selectmenu();
    $("#MYSEL1").on("change", function() {
        var n=$(this).attr("id");
        var dd = $("option:selected", this);
        var d = $(this).val();
        alert (d);  
        $("#MYSEL2").empty(); // remove old options
        if (d=='A') {
            $("#MYSEL2").append('<option value=\"AA\">opt AA</option><option value=\"AAA\">opt AAA</option>');
        } else {
            $("#MYSEL2").append('<option value=\"BB\">opt BB</option><option value=\"BBB\">opt BBB</option>');
        };
    }); 

您可以在jsfiddle中找到:https://jsfiddle.net/t0L915po/5/

1 个答案:

答案 0 :(得分:0)

在对话框打开后,您需要将事件添加到select元素。使用以下代码:

public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(
                             IEnumerable<Attribute> attributes,
                             Type containerType,
                             Func<object> modelAccessor,
                             Type modelType,
                             string propertyName)
    {

        // Call the base method and obtain a metadata object.
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        if (containerType != null)
        {
            // Obtain informations to query the translator.
            //var objectName = containerType.FullName;
            var displayName = metadata.GetDisplayName();

            // Update the metadata from the translator
            metadata.DisplayName = Translator.Translate(displayName);
        }

        return metadata;
    }
}