ボタンを押した後にのみjQueryからダイアログボックスを表示するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1413147

  •  06-07-2019
  •  | 
  •  

質問

このコードは、jQueryサイトのモーダル確認のデモからのものです。

<script type="text/javascript">
$(function() {
    $("#dialog").dialog({
        bgiframe: true,
        resizable: false,
        height:140,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Yes': function() {
                $(this).dialog('close');
            },
            'No': function() {
                $(this).dialog('close');
            }
        }
    });
});
</script>



<div class="demo">

<div id="dialog" title="Empty the recycle bin?">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

<!-- Sample page content to illustrate the layering of the dialog -->
<div class="hiddenInViewSource" style="padding:20px;">
    <p>Sed vel diam id libero <a href="http://example.com">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>
    <form>
        <input value="text input" /><br />
        <input type="checkbox" />checkbox<br />
        <input type="radio" />radio<br />
        <select>
            <option>select</option>
        </select><br /><br />
        <textarea>textarea</textarea><br />
    </form>
</div><!-- End sample page content -->

</div><!-- End demo -->

<div class="demo-description">

<p>Confirm an action that may be destructive or important.  Set the <code>modal</code> option to true, and specify primary and secondary user actions with the <code>buttons</code> option.</p>

</div><!-- End demo-description -->

ボタンを押して初めてこのショーを作る方法を教えてもらえますか?現在、ロード直後にページに自動的に表示されます。

役に立ちましたか?

解決

正しいか間違っているか、私が使用している一般的なアラートスタイルダイアログの例を次に示します。

ダイアログをセットアップします(ドキュメントの準備ができています):

$("#generic-dialog").dialog({
    autoOpen: false,
    buttons: {
        Ok: function() {
            $(this).dialog("close");
        }
    }
});

ダイアログを開きます(何らかのイベントの後):

$("#generic-dialog")
   .text("Status updated successfully.")
   .dialog("option", "title", "Your Status").dialog("open");

他のヒント

ボタンをページに追加し、ボタンのクリックイベントにコードを追加します。

<script type="text/javascript">
$(function() {
    $("#button").click(function(){
    $("#dialog").dialog({
            bgiframe: true,
            resizable: false,
            height:140,
            modal: true,
            overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
            },
            buttons: {
                    'Yes': function() {
                            $(this).dialog('close');
                    },
                    'No': function() {
                            $(this).dialog('close');
                    }
            }
      });
   });
  });
</script>

およびHTML

<input type='button' value="click" id="button"/>
$("#btnTest").click(function() {
       $('#dialog').dialog('open');
        return false;
    })

定義したダイアログが開きます-ボタンのクリックイベントにダイアログを開く関数をアタッチします

ああ-ダイアログ設定プロパティで autoOpen:false を設定します:)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top