2011-05-27 77 views
0

我有这个TextField内的事业部

<div id="mainDiv"> 
    <input type="text" value="Here is the text" /> 
</div> 
<input type="button" value="Push" onclick="changeText()"> 

当我推我想改变文本框从价值的按钮。

只有这个值我想更改,因为在我的页面中有很多文本区域,但这些值必须保持不变。

回答

3

试试这个

<div id="mainDiv"> 
<input type="text" value="Here is the text" /> 
</div> 
<input type="button" value="Push" onclick="document.getElementById('mainDiv').children[0].value='asd'"> 
+1

耶我的第一个答案:) – 2011-05-27 14:03:47

1

那么,也许这将帮助:

<script> 
    function changeText() { 
     var tf = document.getElementById('mainDiv').childNodes[0]; 
     tf.value = 'some text'; 
    } 
</script> 


<div id="mainDiv"><input type="text" value="Here is the text" /></div> 
<input type="button" value="Push" onclick="changeText();"> 
+0

对不起,我忘了添加这条评论div的结构我不能改变。它是从DataTables Jquery生成的。添加一个ID很容易,但不幸的是,我不能这样做 – tinti 2011-05-27 13:46:21

+0

我编辑了我的答案......这有帮助吗? – 2011-05-27 13:51:14

0

尝试与子的id文本框的ID绑定代码 这样的:

<div id="mainDiv"> <input type="text" value="Here is the text" id="input-324" /> </div> <input type="button" value="Push" onclick="changeText(this.id.replace("button-", ""))" id="button-324" > 

<script> 
    function changeText(id) { 
     document.getElementById(id).value='myvalue'; 
    } 
</script> 
+0

对不起,我忘了添加这个评论div的结构我不能改变。它是从DataTables Jquery生成的。 – tinti 2011-05-27 13:45:28

+0

我没有改变结构,我只改变了从changeText()传递给changeText(this)的参数 – VAShhh 2011-05-27 13:49:36

+0

就像我告诉过你的,在我的页面上有很多文本框。尝试添加两个,看看是否可以。 – tinti 2011-05-27 13:54:12

1

您是否正在寻找对于这样的事情?基本上切换输入为静态文本?

Live Demo

function changeText(){ 
    var mainDiv = $("#mainDiv"); 
    mainDiv.text(mainDiv.children('input').val()); 
} 

如果每个DIV多个输入字段该解决方案将工作,

Live Demo 2

function changeText(){ 
    var mainDiv = $("#mainDiv"), 
     inputText = ""; 

    if(mainDiv.children('input').length > 0){ 
     mainDiv.children('input').each(function(){ 
      inputText += $(this).val(); 
     }); 
     mainDiv.text(inputText); 
    } 
}