2012-03-14 6 views
2

ドラッグ可能なリストから項目を削除(複製)できるjQueryのソート可能なリストを使用しています。ユーザーがドロップされた要素のテキストを変更できるようにしたい。だから私は値を表示し、私は隠れた入力フィールドにコピーを保持します。送信者オブジェクトまたはボタンにアクセスしてjQueryダイアログを作成する方法は?

いずれかの要素をクリックすると、jqueryダイアログが開き、テキストがダイアログのテキスト入力に設定されます。終了すると、新しい値が要素テキストと隠れた入力に再び設定されます。

どのような要素がテキストの変更を要求したかをダイアログでどのように知ることができますか?

今、私はその情報を保持するためにグローバル変数を使用しています。しかし、私はそれが醜いと思う。送信者にダイアログを表示するには何らかの方法が必要です。

// Dialog for editing elements 
$('#dialog-form').dialog({ 
    autoOpen: false, 
    modal: true, 
    buttons: { 
     'Ok': function(evt) { 
      val = $('#dialog-form input[name=content]').val(); 
      // Set the text back to the element and the hidden input 
      dialogSender.text(val).prev().val(val); 
      $(this).dialog('close'); 
     }, 
     Cancel: function(evt) { 
      $(this).dialog('close'); 
     } 
    } 
}); 

をそして、あなたが押したボタンはevt.currentTargetになります。

// Global for holding the dialog sender 
var dialogSender = null; 

// Edit element 
$('.element').live('click', function() { 
    dialogSender = $(this); 
    // prev() is the hidden input 
    $('#dialog-form input[name=content]').val($(this).prev().val()); 
    $('#dialog-form').dialog('open'); 
}); 

// Dialog for editing elements 
$('#dialog-form').dialog({ 
    autoOpen: false, 
    modal: true, 
    buttons: { 
     'Ok': function() { 
      val = $('#dialog-form input[name=content]').val(); 
      // Set the text back to the element and the hidden input 
      dialogSender.text(val).prev().val(val); 
      $(this).dialog('close'); 
     }, 
     Cancel: function() { 
      $(this).dialog('close'); 
     } 
    } 
}); 

答えて

2

はあなたのダイアログのウィジェットでクリックされたボタンを関連付けるためにdata()を使用することができ、あなたのOkボタンでその情報をリードバック:あなたは、あなたの「OK」機能に次の行を追加してそれをチェックアウトすることができますハンドラは:

$(".element").live("click", function() { 
    var $this = $(this); 
    // prev() is the hidden input 
    $("#dialog-form input[name=content]").val($this.prev().val()); 
    $("#dialog-form").data("dialogSender", $this).dialog('open'); 
}); 

'Ok': function() { 
    var $this = $(this), 
     val = $("#dialog-form input[name=content]").val(); 
    // Set the text back to the element and the hidden input. 
    $this.data("dialogSender").text(val).prev().val(val); 
    $this.dialog("close"); 
}, 
+1

ありがとう!魅力的な作品! B.t.w前者は非推奨であるため、 'live()'(http://api.jquery.com/live/)を 'on()'(http://api.jquery.com/on/)に変更しました。 – John

0

次の例のように、イベント引数を追加することにより、現在の送信者ボタンを見つけることができます。その後、

console.debug(evt.currentTarget); 

+0

私は、ダイアログのボタンが押されたかを知る必要はありません。とにかく1つのOKボタンしかありません。ダイアログを開くためにどの要素がトリガされたのか知りたい。 – John

+0

同じですが、.live()のリスナー関数で引数 'evt'が追加され、currentTargetが見つかりました。 –

+0

'$( '。element')。live( 'click'、function(evt){console.debug(evt.currentTarget);})' –

関連する問題