How To Get Value of Row Table Appended
i have some problems in my code... I am using html and jquery.. i want to alert value of idPredefineDetail that was appended before... here's my code:
<table id="tablePreDefineDetail">
<tr id="rowPreDefineDetail"><td><input type="checkbox" n开发者_JAVA技巧ame="chkPredefineDetail" ><input type="hidden" name="idPredefineDetail" value="1"></td><td width="5"><input type="text" name="txtCodePredefineDetail" value="ab" size="10"></td><td><input type="text" name="txtDescriptionPredefineDetail" id="txtdesk" size="50"></td><td><a href="#"><button class="upPredefine"></button></a><a href="#"><button class="downPredefine"></button></a><a href="#"><button class="removeStdInsBtn"></button></a></td></tr>
<tr id="rowPreDefineDetail"><td><input type="checkbox" name="chkPredefineDetail" ><input type="hidden" name="idPredefineDetail" value="2"></td><td width="5"><input type="text" name="txtCodePredefineDetail" value="ac" size="10"></td><td><input type="text" name="txtDescriptionPredefineDetail" id="txtdesk" size="50"></td><td><a href="#"><button class="upPredefine"></button></a><a href="#"><button class="downPredefine"></button></a><a href="#"><button class="removeStdInsBtn"></button></a></td></tr>
</table>
When i clicked button remove removeStdInsBtn.. i am using the code below:
$('#tablePreDefineDetail tr button.removeStdInsBtn').click(function(e){
var obj=$(this);
e.preventDefault();
$("#dialog-confirm").dialog({
buttons : {
"Confirm" : function() {
var idPredefineDetail=$(this).parent().parent().children("input[name=idPredefineDetail]").val();
alert(idPredefineDetail);
$(this).dialog("close");
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
$('#dialog-confirm').dialog('open');
});
anybody help me??
You can use a combo of closest and find . Try this:
$('#tablePreDefineDetail tr button.removeStdInsBtn').click(function(e){
var obj=$(this);
e.preventDefault();
$("#dialog-confirm").dialog({
buttons : {
"Confirm" : function() {
var idPredefineDetail=obj.closest("tr").find("input[name=idPredefineDetail]").val();
alert(idPredefineDetail);
$(this).dialog("close");
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
$('#dialog-confirm').dialog('open');
});
You didn't post all your html, but as a first guess, I'd suggest using closest to traverse up the DOM tree.
var idPredefineDetail=$(this).closest( "#dialog-confirm" ).children("input[name=idPredefineDetail]").val();
I believe the $(this) is actually pointing to #dialog-confirm. You should use the obj variable that you set at the start of the click callback function. You also didn't go back far enough, you need another parent() call.
var idPredefineDetail=obj.parent().parent().parent().children("input[name=idPredefineDetail]").val();
alert(idPredefineDetail);
$(this).dialog("close");
well here i want to modify the below mentioned code which was originally posted by Cybernate
$('#tablePreDefineDetail tr button.removeStdInsBtn').click(function(e){ var obj=$(this);
e.preventDefault();
$("#dialog-confirm").dialog({
buttons : {
"Confirm" : function() {
var idPredefineDetail=obj.attr("id");
alert(idPredefineDetail);
$(this).dialog("close");
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
$('#dialog-confirm').dialog('open');
});
for example i have a hyperlink like this.
clickme
when i click this link the value which i access is id='clickit' as obj.attr("id"); is used. so we can place a required value in id, or in name or in another tag property and later we can easily acces it...
精彩评论