简体   繁体   English

如何在单击按钮时保留先前的文本框值

[英]How to remain the previous text box value on button click

Hello Friends I am facing some problem or can say not figure out any help regard this so please help me There is button having name Add Text when I click that button it display a text box when i enter some text in this text box it display the entered text below.when I click again Add Text button then previous one textbox seems empty i don't want this. 您好朋友,我遇到了一些问题,或者不能说没有任何帮助,请帮我。有一个按钮名为“添加文本”,当我单击该按钮时,它会显示一个文本框;当我在此文本框中输入一些文本时,它会显示在下面输入文本。当我再次单击“添加文本”按钮时,上一个文本框似乎为空,我不希望这样做。

1.) My requirement is when I click Add Text button second time it doesn't empty the previous textbox. 1.)我的要求是,当我第二次单击“添加文本”按钮时,它不会清空前一个文本框。

2.) when i click the B button then it convert the text to bold.Right now its working for one text i want when i select particular text and click B button it convert the particular selected text to bold. 2.)当我单击B按钮时,它将文本转换为粗体。现在,当我选择特定文本并单击B按钮时,它适用于我想要的一种文本,将其转换为粗体。 These are my two problem.Please reply if anybody having the solution of my problem. 这是我的两个问题。如果有人解决了我的问题,请回复。 Thanks 谢谢

<script type="text/javascript">
   var i=0;
   function add_input()
        {
          if(i!=6){
              var d=document.getElementById('div');

           d.innerHTML+="<input type='text'  name='addtext"+i+"' id='addtext"+i+"'onkeyup='copy("+i+")'><br></br>";
                 i++;
                 }

        }
  function copy(j){

           var mm = 'addtext'+j;
           var divid = 'newcontent'+j;
           var s1=document.getElementById(mm).value;

           /*var s2=document.getElementById('newcontent'+j);*/
           var s2=document.getElementById('newcontent'+j);

           s2.innerHTML=s1;


            }
          </script>


      <script type="text/javascript">
          function f2() {

               if(document.getElementById('newcontent0').style.fontWeight =='bold')
                    {
                         document.getElementById('newcontent0').style.fontWeight = 'normal';
                    }
                   else if(document.getElementById('newcontent0').style.fontWeight =='normal')
                     {
                        document.getElementById('newcontent0').style.fontWeight = 'bold';
                      }
                  else if(document.getElementById('newcontent0').style.fontWeight =='')
                     {
                        document.getElementById('newcontent0').style.fontWeight = 'bold';
                      }
              }
             </script>

     <input  type="button" value="Add Text" onclick="add_input()"  />
     <input type="button"  value="B"  onclick="f2()"/> 
     <div id="div"></div>
     <div id="newcontent0"  style="padding:0px;margin:0px;"></div>
     <div id="newcontent1"  style="padding:0px;margin:0px;"></div>
     <div id="newcontent2"  style="padding:0px;margin:0px;"></div>
     <div id="newcontent3"  style="padding:0px;margin:0px;"></div>
     <div id="newcontent4"  style="padding:0px;margin:0px;"></div>

Thanks 谢谢

This will solve you first problem 这将解决您的第一个问题

function add_input()
        {
          if(i!=6){
              var d=document.getElementById('div');
          p = document.createElement("input");

           p.type="text";
           p.name="addtext"+i ;
           p.id="addtext"+i ;
           p.setAttribute('onkeyup','copy('+i+')');    
           br = document.createElement("<br>");
           d.appendChild(p);
           d.appendChild(br);
                 i++;
                 }

        }

for second check out this link may help you http://www.java2s.com/Code/JavaScript/HTML/CapturingaTextSelection.htm 对于第二次检查,此链接可能会帮助您http://www.java2s.com/Code/JavaScript/HTML/CapturingaTextSelection.htm

1.) My requirement is when I click Add Text button second time it doesn't empty the previous textbox. 1.)我的要求是,当我第二次单击“添加文本”按钮时,它不会清空前一个文本框。

This happens because you replace the innerHTML . 发生这种情况是因为您替换了innerHTML Any text you have typed into the input field will not be retained, because it's not part of the new innerHTML . 您在输入字段中键入的所有文本都不会保留,因为它不是新的innerHTML一部分。

To prevent this from happening, you can manipulate the DOM instead. 为防止这种情况发生,您可以改为操作DOM。 It may be a bit more code and theoretically somewhat slower, but in this case that's not something you need to worry about. 它可能需要更多的代码,并且从理论上讲会稍慢一些,但是在这种情况下,您不必担心。

2.) when i click the B button then it convert the text to bold.Right now its working for one text i want when i select particular text and click B button it convert the particular selected text to bold. 2.)当我单击B按钮时,它将文本转换为粗体。现在,当我选择特定文本并单击B按钮时,它适用于我想要的一种文本,将其转换为粗体。

This one is a little trickier. 这有点棘手。 We need to remember which input field was last selected, so that f2() can set the fontWeight of the correct element. 我们需要记住最后一个选择的输入字段,以便f2()可以设置正确元素的fontWeight。 You can do that by using the onblur event of the input elements and having a variable lastFocused remembering which one was last selected. 您可以通过使用输入元素的onblur事件并使变量lastFocused记住上一次选择哪个变量来lastFocused这一点。

All in all, something like this should work (try here: http://jsfiddle.net/2euLZ/ ) : 总而言之,类似这样的事情应该起作用(在这里尝试: http : //jsfiddle.net/2euLZ/ ):

var i = 0;
var lastFocused = null;

function add_input()
{
    if(i != 6){
        var d=document.getElementById('div');
        var newId    = i;

        var newInput = document.createElement("input");
        newInput.type    = "text";
        newInput.name    = "addtext" + i;
        newInput.id      = "addtext" + i;
        newInput.onkeyup = function() { copy(newId); };
        newInput.onblur  = function() { lastFocused = newId; };

        d.appendChild(newInput);
        d.appendChild(document.createElement("br"));
        i++;
    }
}

function copy(j)
{
       var s1 = document.getElementById('addtext'+j);
       var s2 = document.getElementById('newcontent'+j);
       s2.innerHTML = s1.value;
}

function f2() {
    var target = document.getElementById("newcontent" + lastFocused);

    var newFontWeight = "bold";
    if(target.style.fontWeight == "bold")
        newFontWeight = "normal";

    target.style.fontWeight = newFontWeight;
}

Your problem is a consequence of resetting the innerHTML every time, which has the side effect of resetting the contents of the text boxes. 您的问题是每次都要重置innerHTML的结果,这具有重置文本框内容的副作用。 You probably did not intend to delete and recreate all the text boxes when just one is added. 仅添加一个文本框时,您可能不打算删除并重新创建所有文本框。 Instead, use DOM methods to manually build nodes and insert them: 而是使用DOM方法手动构建节点并插入它们:

var inp = document.createElement('input');
inp.name = 'addtext' + i;
inp.id = 'addtext' + i;
inp.onkeyup = function() {
    copy(this.id.slice(7));
};
d.appendChild(inp);
d.appendChild(document.createElement('br');

JavaScript libraries can make this easier. JavaScript库可以使此过程更容易。 In fact, jQuery can render your exact innerHTML generation attempt functional using only $('#div').append('your html code here') . 实际上,jQuery可以只使用$('#div').append('your html code here')来使您的完全innerHTML生成尝试发挥作用。 If you do not use jQuery, insertAdjacentHTML() might suffice. 如果不使用jQuery, insertAdjacentHTML()可能就足够了。

As far as getting the selected text goes, take a look at the fieldSelection code. 就获取所选文本而言,请查看fieldSelection代码。 It's old and may not work with current versions of jQuery, but that's roughly it. 它很旧,可能无法与当前版本的jQuery一起使用,但是仅此而已。 If you are also planning on implementing italic text, underlining, etc. , you may want to consider a contentEditable div as used by the major in-browser rich text editing scripts such as CKEditor (although that particular one uses an iframe). 如果您还计划实现斜体文本,下划线 ,则可能需要考虑由主要的浏览器内富文本编辑脚本(例如CKEditor)使用的contentEditable div (尽管该特定脚本使用iframe)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM