C# 使用Jquery在repeater中动态查找控件
我很难在中继器控件中找到文本框值。在按钮点击时,我需要得到对应于文本框的文本框值,该文本框位于被点击的按钮的内部 因此,我: Textbox1-->必须用按钮1绑定 TextBox2-->,它与按钮2绑定C# 使用Jquery在repeater中动态查找控件,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我很难在中继器控件中找到文本框值。在按钮点击时,我需要得到对应于文本框的文本框值,该文本框位于被点击的按钮的内部 因此,我: Textbox1-->必须用按钮1绑定 TextBox2-->,它与按钮2绑定 <span>Question :</span><span>my second content <br /></span><span>Answer : </span><input name="ctl00$M
<span>Question :</span><span>my second content <br /></span><span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="6454" />
我让它部分工作与:
$(this).siblings('input[type="text"]').first().val("clicked");
在这里我得到设置文本框文本,但再次,如果我点击第二个按钮,什么都没有发生
我做错了什么?如何从特定文本框中获取值
更新
现在,我使用的解决方案是向每个控件添加自定义属性:
<span>Question :</span>
<span>my second content <br /></span>
<span>Answer : </span>
<input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="654" mydataid="2" />
<button id="MainContent_ctl00_answerQuestion_0" Class="addAnswerButton" onclick="Click()" mydataid="2" >Add Question</button><hr /><span>Question :</span><span>abcdedf <br /></span>
<span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl01$TextArea" type="text" mydataid="1" id="MainContent_ctl00_TextArea_1" value="654" />
<button id="MainContent_ctl00_answerQuestion_1" Class="addAnswerButton" onclick="Click()" mydataid="1">Add Question</button>
但是,如何获得将samme自定义属性名称作为按钮的textbox控件
更新2
70次尝试后,我终于找到了答案(我第一次尝试Jquery)
解决方案
$(".addAnswerButton").click(function() {
var pos = $(this).attr("mydataid");
results = $('input[type = "text"][mydataid="'+pos+'"]').first();
alert(results.val());
});
关键是,您将获得自定义属性“name”,然后搜索具有相同属性名称的文本框 您可以在服务器上用一些属性标记它们,并在jQuery代码中使用它 服务器的输出如下所示:
<input type='text' data-item-id='1'>First</input>
<inpyt type='button' data-item-id='1'></input>
<input type='text' data-item-id='2'>Second</input>
<inpyt type='button' data-item-id='2'></input>
首先
第二
按下按钮后,获取其数据项id并搜索具有相同属性值的文本框。在注释后编辑 我将前往您的JSFIDLE,并使其与以下内容一起工作:
$('[id*="answerQuestion"]').click(function () {
$(this).parent().children('[id*="TextArea_' + $(this).attr('id').substring($(this).attr('id').length - 1) + '"]').val('clicked');
});
说明:
您好,如果有一些英语不一致,请道歉:) 你能展示你的html结构吗?它们都是相对元素的同级还是仅仅是同级?你能用html和jquery创建一个js吗?fiddler:我不能这样做,因为id是动态加载的,从代码behinde动态加载重复,你能演示如何添加数据项id吗?
<input type='text' data-item-id='1'>First</input>
<inpyt type='button' data-item-id='1'></input>
<input type='text' data-item-id='2'>Second</input>
<inpyt type='button' data-item-id='2'></input>
$('[id*="answerQuestion"]').click(function () {
$(this).parent().children('[id*="TextArea_' + $(this).attr('id').substring($(this).attr('id').length - 1) + '"]').val('clicked');
});