2011-12-21 112 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