$('input[name=A01]').on('keyup', function() {
var val = $.trim( this.value ),
x = 'your_custom_value',
id = this.id,
target = $('label[for='+ id +'_1], input[name='+ id +'_1]');
target.hide();
if( val == x) {
target.show();
}
});
为了重用,您可以更改标记,例如:
<form id="survey">
<fieldset>
<label for="A01">A01</label>
<input type="number" name="A01" id="A01" min="0" max="5" class="myinput">
<label for="A01_1">A01_1</label>
<input type="text" name="A01_1" id="A01_1" >
<label for="A02">A02</label>
<input type="number" name="A02" id="A02" min="0" max="5" class="myinput">
<label for="A02_1">A02_1</label>
<input type="text" name="A02_1" id="A02_1" >
</fieldset>
</form>
并像这样改变jQuery:
$('input.myinput').on('keyup', function() {
var val = $.trim( this.value ),
x = 'your_custom_value',
id = this.id,
target = $('label[for^='+ id +'_], input[name='+ id +'_]');
target.hide();
if( val == x) {
target.show();
}
});