2011-12-21 9 views
1

私はJQueryダイアログを持っていますが、その中にテキストボックスを含むコントロールがあります。また、okボタンもあります。 OKをクリックすると、テキストボックスから値が取得され、関数に渡されます。ダイアログボックスのテキストボックスから古い値を取得する

問題:私は、元の値が3であれば、私は20に変更し、そう、それが唯一の古い値を取得し、テキストボックスに

を値を変更

毎回、私はボタンをクリックして、それ値を取得します。

これはなぜですか?

ありがとうございました!ここ

はいくつかのコードです:

のjQuery:

$("#addtxt").click(function (e) { 
    $("#dialog").show('slide'); 
    $("#dialog").html('<input id="my_txttext" name="my_txttext" title="Manoj" type="text" label="Add Text" />'); 
    $("#dialog").dialog({ 
     resizable: false, 
     modal: true, 
     position: { 
      my: 'center', 
      at: 'center', 
      // collision: 'fit', 
      // ensure that the titlebar is never outside the document 
      using: function (pos) { 
       var topOffset = $(this).css(pos).offset().top; 
       if (topOffset < 0) { 
        $(this).css('top', pos.top - topOffset); 
       } 
      } 
     }, 
     width: 300, 
     CloseText: '', 
     title: 'Add Text', 
     buttons: { 
      'OK': function() { 
       //to create dynamic div to contain data 
       var div = document.createElement('div'); 
       $(div).attr("id", "dyndiv" + count); 
       objid = "dyndiv" + count; 
       count++; 
       $('#sel_obj_text').val("Text"); 
       text_visibility(); 

       var $ctrl = $(div).text($('#my_txttext').get(0).value).addClass("draggable ui-widget-content HandleTopRowBorder").draggable({ 
        containment: '#containment-wrapper', 
        cursor: 'move', 
        snap: '#containment-wrapper' 
       }); 

       $("#containment-wrapper").append($ctrl); 
       $('#' + objid).position({ 
        of: $("#containment-wrapper"), 
        my: "center" + " " + "center", 
        at: "center" + " " + "center" 
       }); 
       // $('#my_txttext').val('') 
       $(this).dialog("destroy"); 

      }, 
      'Cancel': function() { 
       $(this).dialog("destroy"); 
       // I'm sorry, I changed my mind     
      } 
     } 

    }); 
+0

私のjQuery-FUは素晴らしいではありませんので、私はここに肯定的ではないんだけど、あなたが呼んでいる理由は、私は次のようではありませんよget(0)...もしあなたがmy_txttextの値を望むならば、あなたは '$( '#my_txttext).val'を実行できませんか? – DaOgre

+0

実際に$( '#my_txttext).val()を使用していましたが、同じ問題があります –

+0

「テキストボックスの値を変更するたびに」と言うと、ダイアログボックスを複数回開いているということですか?値を変更しますか? – jessegavin

答えて

1
$("#addtxt").click(function (e) { 
    var $input = $('<input title="Manoj" type="text" placeholder="Add Text" />'); 
    $("#dialog") 
    .empty() 
    .append($input) 
    .show('slide') 
    .dialog({ 
     resizable: false, 
     modal: true, 
     position: { 
      my: 'center', 
      at: 'center', 
      // collision: 'fit', 
      // ensure that the titlebar is never outside the document 
      using: function (pos) { 
       var topOffset = $(this).css(pos).offset().top; 
       if (topOffset < 0) { 
        $(this).css('top', pos.top - topOffset); 
       } 
      } 
     }, 
     width: 300, 
     CloseText: '', 
     title: 'Add Text', 
     buttons: { 
      'OK': function() { 

       $('#sel_obj_text').val("Text"); 
       text_visibility(); 

       $("<div>", { 'id' : "dyndiv" + count }) 
       .text($input.val()) 
       .addClass("draggable ui-widget-content HandleTopRowBorder") 
       .draggable({ 
        containment: '#containment-wrapper', 
        cursor: 'move', 
        snap: '#containment-wrapper' 
       }) 
       .appendTo("#containment-wrapper") 
       .position({ 
        of: $("#containment-wrapper"), 
        my: "center" + " " + "center", 
        at: "center" + " " + "center" 
       }); 

       $(this).dialog("destroy"); 

       count++; 
      }, 
      'Cancel': function() { 
       $(this).dialog("destroy"); 
      } 
     } 

    }); 
+0

ありがとうございました。私のコードで何が間違っていたのですか?私はダイアログに直接コントロールを追加すると思います。 –

+0

私の理論は、何とか複数の '要素がDOMに追加されていて、' $( 'my_txttext').get(0).value'を呼び出していたときに、最初の値。 – jessegavin

+0

しかし..私は無駄にその動作を再現するデモを作成しようとしました。だから、なぜ私のソリューションが動作するのかを正確に伝えることはできないと思う。しかし、私はそれがうれしい。 – jessegavin

関連する問題