2011-12-21 56 views
1

我有一個JQuery對話框,裏面有一個包含文本框的控件,裏面還有一個ok按鈕。在確定點擊我從文本框中獲取值並將其傳遞給函數。從對話框中的文本框中獲取舊值

問題:

每次我改變它只是變得老值文本框中的值,

所以,如果原來的值是3,我將其更改爲20,我按一下按鈕,它得到值3.

任何人有任何想法,爲什麼它這樣做?

謝謝!

這裏是一些代碼:

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福不是驚人的,但我不是下面你爲什麼打電話get(0)...如果你想my_txttext的值不能只是'$('#my_txttext).val'? – DaOgre 2011-12-21 18:15:26

+0

其實早些時候我使用$('#my_txttext).val(),但它也有同樣的問題 – 2011-12-21 18:24:50

+0

當你說「每次我改變文本框中的值」時,你的意思是你多次打開對話框,改變價值? – jessegavin 2011-12-21 18:27:05

回答

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

非常感謝。我的代碼出了什麼問題。我想直接添加控件到對話框。 – 2011-12-21 18:55:40

+0

我的理論是,不知何故多個'>元素被添加到DOM,當你調用'$('#my_txttext')。get(0).value'時,jQuery只返回第一個的值。 – jessegavin 2011-12-21 19:37:01

+0

但是..我試圖創建一個演示來重現這種行爲沒有用。所以我不認爲我能夠準確地告訴你爲什麼我的解決方案能夠工作......但我很高興它能做到。 – jessegavin 2011-12-21 19:37:51