我有以下HTML代码: -
<!DOCTYPE html>
<html>
<body>
<fieldset>
<legend>
<button>
Show/Hide form
</button>
</legend>
Name:
<input type="text" />
<br />
Email:
<input type="text" />
<br />
Date of birth:
<input type="text" />
</fieldset>
<fieldset>
<legend>
<button>
Show/Hide form
</button>
</legend>
Name:
<input type="text" />
<br />
Email:
<input type="text" />
<br />
Date of birth:
<input type="text" />
</fieldset>
<fieldset>
<legend>
<button>
Show/Hide form
</button>
</legend>
Name:
<input type="text" />
<br />
Email:
<input type="text" />
<br />
Date of birth:
<input type="text" />
</fieldset>
</body>
</html>
但是,如果用户点击“显示/隐藏表单”按钮,我如何显示和隐藏字段集?是否有办法根据字段的当前状态将按钮标签更改为显示或隐藏组? BR
答案 0 :(得分:3)
隐藏fieldset
时,您的按钮也会隐藏。您可以修改标记并使用toggle
方法:
<button class='toggle'>Hide</button>
<fieldset>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
</fieldset>
$('.toggle').click(function(){
var $this = $(this);
$this.text( $this.text() == 'Show' ? "Hide" : "Show" )
$this.next().toggle()
})
答案 1 :(得分:0)
您的HTML代码:
<button>Show/Hide form</button>
<fieldset>
<legend></legend>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
</fieldset>
<br />
<button>Show/Hide form</button>
<fieldset>
<legend></legend>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
</fieldset>
<br/>
<button>Show/Hide form</button>
<fieldset>
<legend></legend>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
</fieldset>
你的jQuery代码:
$(function(){
$('button').click(function(){
if( $(this).html()=='Show/Hide form') $(this).html('Hide');
$(this).nextAll('fieldset:first').toggle();
$(this).html()=='Show'?($(this).html('Hide')):($(this).html('Show'));
});
})
你的JSFIDDLE: