Javascript 如何使用jquery访问同一“表td”中的div id
我有以下代码:Javascript 如何使用jquery访问同一“表td”中的div id,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我有以下代码: <td> <div id="div<%# Eval("Id") %>" class="Display"><%# Eval("Display") %></div> <div class="Actions"> </div> <div class="Comment"> <span>Comment </span> <input id="
<td>
<div id="div<%# Eval("Id") %>" class="Display"><%# Eval("Display") %></div>
<div class="Actions">
</div>
<div class="Comment">
<span>Comment </span>
<input id="txt<%# Eval("Id") %>" type="text" width="400px" />
</div>
</td>
注意:这是itemtemplate,因此每个div和输入元素都有不同的id
您基本上需要触发enter键按下事件,并将值从框1复制到框2。对输入标记进行一些修改
<script>
function enterPress(evt,input) {
if(evt.keyCode==13){
var divHtml=document.getElementById('div<%# Eval("Id") %>').innerHTML;
var inputValue=input.value;
inputValue=inputValue+divHtml;
document.getElementById(input.id).value=inputValue;
}
}
</script>
<input id="txt<%# Eval("Id") %>" type="text" width="400px" onkeypress="enterPress(event,this)"/>
<div id="div<%# Eval("Id") %>" class="Display"><%# Eval("Display") %></div>
<input id="txt<%# Eval("Id")%>" class="<%# Eval("Id") %>" type="text" width="400px" />
希望能有所帮助。到目前为止,你都在尝试什么??请粘贴你的代码。好的,我编辑了我的问题
if (e.which == 13) {
var comment = $("#" + this.id).val();
var textId = "#" + this.id.replace("txt", "div");
$(textId).append(comment);
$.ajax({
type: "POST",
url: "Home.aspx/AddComments",
data: "{'id': '" + this.id.replace("txt", "") + "','comments': '" + comment + "'}",
dataType: "json",
contentType: "application/json",
success: function (response) {
alert(response.d);
}
});
}
var testTextBox = $('#txt<%# Eval("Id") %>');//your input id
var targetbox=$('div<%# Eval("Id") %>');//your second box.
var code =null;
testTextBox.keypress(function(e)
{
code= (e.keyCode ? e.keyCode : e.which);
if (code == 13) {//enter key
$(targetbox).text($(tstTextBox).val());
}
e.preventDefault();
});
<script>
function enterPress(evt,input) {
if(evt.keyCode==13){
var divHtml=document.getElementById('div<%# Eval("Id") %>').innerHTML;
var inputValue=input.value;
inputValue=inputValue+divHtml;
document.getElementById(input.id).value=inputValue;
}
}
</script>
<input id="txt<%# Eval("Id") %>" type="text" width="400px" onkeypress="enterPress(event,this)"/>
<div id="div<%# Eval("Id") %>" class="Display"><%# Eval("Display") %></div>
<input id="txt<%# Eval("Id")%>" class="<%# Eval("Id") %>" type="text" width="400px" />
$(function(){
$(".comment :input").keypress(function(e) {
if(e.which == 13)
{
var idNo =$(this).prop("class");
var comment = $("#text" + idNo).val();
$("#div"+idNo).append(comment);
$.ajax({
type: "POST",
url: "Home.aspx/AddComments",
data: "{'id': '" + idNo + "','comments': '" + comment + "'}",
dataType: "json",
contentType: "application/json",
success: function (response) {
}
});
}
e.preventDefault();
});
});