jquery对话框结果未更新控件

时间:2014-01-31 22:44:29

标签: jquery-ui-dialog

关闭jqueryui.dialog时,我正在更新文本框的值。它在文本框中显示值,但是当文本框获得焦点时,该值将消失。我只是想不通原因。文本框中没有onfocus / onblur事件。

我正在更新像这样的文本框值

$("#mycontrol").val(displayresult);


$(html).dialog({
    modal: true,
    height: 300,
    overflow: scroll,
    buttons: {
        "Select": function () {
            $(this).dialog("close");
            var value, splitPosition, itemValues, displayValues;
            var displayresult = "";
            var itemresult = "";
            var septex = "";
            var sepval = "";
            $("input:checkbox" && "[id^=" + controlName + "_selectionBoxCheckBox]").each(function () {
                var $this = $(this);
                if ($this.is(":checked")) {
                    value = $this.val();
                    splitPosition = value.indexOf("-");
                    itemValues = $this.val().substr(0, splitPosition);
                    displayValues = $this.val().substr(splitPosition + 1);

                    itemresult += sepval + itemValues;
                    displayresult += septex + displayValues;
                    sepval = ",";
                    septex = ", ";
                }
            });

            $("#mycontrol").val(displayresult);
            $(this).dialog('destroy').remove();
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    },
    close: function () {
        //alert('close');
    },
    beforeClose: function (event, ui) {
        //alert("before close");
    }
});

1 个答案:

答案 0 :(得分:0)

我用一些HTML代码包装它,它工作正常。必须有一些其他代码导致问题。

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
</head>

<body>

  <form>
    <input type="text" id="mycontrol" />
  </form>

  <div id="dialog" title="Dialog Title">
    one <input type="checkbox" id="cb_selectionBoxCheckBox1" value="1-one" /><br />
    two <input type="checkbox" id="cb_selectionBoxCheckBox2" value="2-two" /><br />
    three <input type="checkbox" id="cb_selectionBoxCheckBox3" value="3-three" /><br />
  </div>

  <script type="text/javascript">
    var displayresult="initial";
    var controlName="cb";
    var html='#dialog';
    $().ready(function(){

    $("#mycontrol").val(displayresult);

    $(html).dialog({
        modal: true,
        height: 300,
        overflow: scroll,
        buttons: {
        "Select": function () {
            $(this).dialog("close");
            var value, splitPosition, itemValues, displayValues;
            var displayresult = "";
            var itemresult = "";
            var septex = "";
            var sepval = "";
            $("input:checkbox" && "[id^=" + controlName + "_selectionBoxCheckBox]").each(function () {
            var $this = $(this);
            if ($this.is(":checked")) {
                value = $this.val();
                splitPosition = value.indexOf("-");
                itemValues = $this.val().substr(0, splitPosition);
                displayValues = $this.val().substr(splitPosition + 1);

                itemresult += sepval + itemValues;
                displayresult += septex + displayValues;
                sepval = ",";
                septex = ", ";
            }
            });

            $("#mycontrol").val(displayresult);
            $(this).dialog('destroy').remove();
        },
        Cancel: function () {
            $(this).dialog("close");
        }
        },
        close: function () {
        //alert('close');
        },
        beforeClose: function (event, ui) {
        //alert("before close");
        }
    });  

    });
</script>
</body>
</html>