问题
In the below code how to validate for null values for only the dynamically added text box i.e,r_score1,r_score2.........
<script>
function validate()
{
//How to validate the r_Score.. textfields
}
$(document).ready(function() {
for(var i=0;i< sen.length;i++)
{
row += '<input type="text" name="r_score'+i+'" id="r_score'+r_count+'" size="8" />';
}
$('#details').append(row);
});
</script>
<div id="details"><input type="text" name="score' id="score" size="8" /></div>
<input type="button" value="Save" id="print" onclick="javascript:validate();" />
回答1:
Give the text boxes a class name and use class selector
function validate()
{
var boxes = $("#details input:text.classname");
boxes.each(function(){
var currentVal = $(this).val();
// do your validation here
});
}
回答2:
$("#details input[name^='r_score']").each(function(nr){
if($(this).val() === "") alert("Field "+(nr+1)+" is empty");
});
回答3:
Use live event handler for dynamically added content.
来源:https://stackoverflow.com/questions/2682030/validate-dynamically-added-textbox