一个相当直接的方法:
/* Cache selectors, and set limitation variables */
var adults = $("#adults"),
infant = $("#infants"),
msgDiv = $("#mydiv"),
vLimit = 10;
/* Bind to adults and infants some onChange logic */
$(adults).add(infant).on("change", function(){
/* Determine values, addition, and results */
var aValue = parseInt( adults.val(), 10 ),
iValue = parseInt( infant.val(), 10 ),
result = aValue + iValue,
differ = result - vLimit;
/* If the user selected too many, inform them */
if ( differ > 0 ) {
/* Invalid amount: x too many. */
msgDiv.html("Invalid amount: %d too many.".replace( /%d/, differ ));
return;
}
/* Clear msgDiv, fill it with x input elements */
var i = 0; msgDiv.empty();
while ( i++ < result ) $("<input/>", { id: 'element'+i }).appendTo(msgDiv);
});
演示:http: //jsbin.com/etawas/8/edit