在我的例子中如何动态更改 HTML 元素?
我的页面上有一组单选按钮:
<form ...>
<input type="radio" name="people" checked> Student
<input type="radio" name="people"> Teacher
<input type="radio" name="people"> Assistant
<!-- Here is the dynamic content, which could be check boxes or radio buttons-->
</form>
我想要实现的功能是:
- 根据单选按钮的选择,单选按钮后面的内容将动态变化。 (单选按钮和内容位于表单内。)
例如:
如果选择“学生”,则动态内容部分为(复选框):
姓名
> <输入类型=“复选框”名称=“年龄”/>年龄
> <输入类型=“复选框”名称=“等级”/>等级
如果选择“教师”,则动态内容部分为(复选框):
如果选择“教师”,则动态内容部分是(复选框和单选按钮):
主题
> <输入类型=“无线电”名称=“代码”已选中> 111 <输入类型=“无线电”名称=“代码”> 222 <输入类型=“无线电”名称=“代码”>第333章如果选择“助理”
如何在jQuery中实现这种动态内容更改?
我尝试
在 Javascript 中动态创建 HTML 元素,但我觉得这不是一个好方法,因为我必须在 Javascript 中将 HTML 元素编写为字符串。
I have a group of radio buttons on my page:
<form ...>
<input type="radio" name="people" checked> Student
<input type="radio" name="people"> Teacher
<input type="radio" name="people"> Assistant
<!-- Here is the dynamic content, which could be check boxes or radio buttons-->
</form>
The feature I would like to implement is:
- Based on the selection of the radio buttons, the content after the radio buttons will change dynamically. (The radio buttons and the content are inside a form.)
For example:
If "student" is selected, the dynamic content part is (check boxes):
<input type="checkbox" name="name" /> Name <br /> <input type="checkbox" name="Age" /> Age <br /> <input type="checkbox" name="grade" /> Grade <br />
If "Teacher" is selected, the dynamic content part is (check boxes & radio buttons):
<input type="checkbox" name="subject" /> Subject <br /> <input type="radio" name="code" checked> 111 <input type="radio" name="code"> 222 <input type="radio" name="code"> 333
If "Assistant" is selected, the dynamic content part is other check boxes.
How to implement this dynamic content change in jQuery?
What I tried
I tried to create HTML elements dynamically in Javascript, but I feel it is not a good way since I have to write HTML elements in Javascript as strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
工作 演示
标记更改
Js
Try this
Working demo
Markup change
Js
将所有三个可能的元素放入静态 html 中,并用 div 包裹每个部分。然后单击时显示和隐藏 div
put all three possible elements in your static html and wrap each part with a div. Then Show and hide the divs on click
如果我正确理解你的问题...我会创建隐藏的不同 div,其中包含你正在寻找的组合。然后,在单击单选按钮时,我将隐藏您不想显示的 div 并显示您正在查找的 div。
If I understand your question correctly... I would create different divs that are hidden that contain the combinations that you are looking for. Then on the onclick of the radio button I would hide divs that you don't want shown and show the divs that you are looking for.