我有一个系统,当按下按钮时,在屏幕左侧创建一个文本框并将其包围在一个div中。每个新的文本框命名为textbox1,2等。我想要做的是当用户点击围绕它的div,然后文本框替换屏幕右侧的按钮,用户可以键入到新生成的文本框它也应填充屏幕左侧的文本框。我想这样做的原因是因为左侧的文本框不可编辑,除非用户点击div并编辑右侧的框。我似乎无法弄清楚如何去做。在div中更改动态生成的文本框的内容
请原谅在遗失标签等任何错误,因为这是一个严格编辑的版本。在查看下面的代码之前,您可能想看看我设置的演示。 http://jsfiddle.net/JHmaV/335/。
下面是HTML代码
<html>
<body>
<table width ="100%" alight="right">
<td width ="51.5%"></td>
<td> <div id="addField" class="tabOptions">Add field</div></td>
</table>
<table align ="left"style="background-color: white" width="100%">
</tr>
<td width ="50%">
<ul id ="firstColumn">
<div id="identifier">
</div>
</ul>
</td>
<td>
<ul id ="secondColumn" width="5%">
<div id="placeholder">
<div id="mainPanel">
<li><input type="button" class="formCreationButton" id="textAdd" value="Single line text" /></li>
</div>
</div>
</ul>
</td>
<tr>
</table>
这里是jQuery的
var counter = 0;
var textBoxCounter = 1;
var tempStorage = $('div#placeholder').html();
function setValue(target) {
$("textBox1").replaceWith("<h2><input type='text' onkeydown='setValue(this)' value='"+ target.value +"' id='" + target.id +"' name='" + target.id +"')'></h2>");
target.value = target.value;
}
function changeHeader(target) {
if(target.id == "formNameChange"){
$('h2').replaceWith('<h2>' +target.value + '</h2>');
}
else{
$('h4').replaceWith('<h4>' +target.value + '</h4>');
}
}
$(document).ready(function() {
$("#addField").live('click','div',function(){
$('div#placeholder').html(tempStorage);
});
$('#textBox1').live('keyup',function() {
alert("TESTING");
});
$('#textBox1').live('keypress',function()
{
$('textBox1').val('Changed Value');
alert('test');
});
$('#textBox1').live('onFocus',function()
{
alert('test');
});
$(".container").live('click','div',function(){
var divID = this.id;
if(divID != ""){
var lastChar = divID.substr(divID.length - 1);
var content = document.getElementById(divID).outerHTML;
var text = document.getElementById(divID).innerHTML;
var textboxId = $('div.container')
.find('input[type="text"]')[lastChar]
.id;
$('div#placeholder').html(content);
alert(textboxId);
}
else{
}
});
$('#textAdd').live('click',function() {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Textbox " + textBoxCounter + " <br><div id='container " + counter + "' class='container'><li><input type='text' onkeydown='setValue(this)' id='textBox " + textBoxCounter +"' name='textBox " + textBoxCounter +"')'></li></div></br>";
document.getElementById("identifier").appendChild(newdiv);
textBoxCounter++
counter++;
});
});
请注意,我使用jQuery 1.4.4。我曾多次尝试过,但无法正确执行。任何帮助将不胜感激,提前谢谢。 保罗。
的空间更加公正,来自全国各地复制错误。是的,这就是我打算用同样的名字。我希望用户将文本输入到应该复制到旧文本框中的新创建的框中。我应该创建一个新的文本框并尝试以这种方式复制内容吗?我能看到的唯一问题是跟踪左侧的框,而右侧的框正在被编辑,以便内容同时改变。对不起,如果我没有得到有效的需要。谢谢回复! – Chaney 2012-07-16 16:49:31
@Chaney用解决方案更新了我的帖子:) – 2012-07-16 17:25:09
非常好!非常感谢你,先生,这正是我需要的!谢谢! – Chaney 2012-07-16 17:38:36