在我的例子中如何动态更改 HTML 元素?

发布于 2024-11-30 07:58:16 字数 1178 浏览 0 评论 0原文

我的页面上有一组单选按钮:

<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>

我想要实现的功能是:

  • 根据单选按钮的选择,单选按钮后面的内容动态变化。 (单选按钮和内容位于表单内。)

例如:

  1. 如果选择“学生”,则动态内容部分为(复选框):

    姓名 
    > <输入类型=“复选框”名称=“年龄”/>年龄
    > <输入类型=“复选框”名称=“等级”/>等级
  2. 如果选择“教师”,则动态内容部分为(复选框):

    如果选择“教师”,则动态内容部分是(复选框和单选按钮):

    主题 
    > <输入类型=“无线电”名称=“代码”已选中> 111 <输入类型=“无线电”名称=“代码”> 222 <输入类型=“无线电”名称=“代码”>第333章
  3. 如果选择“助理

如何在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:

  1. 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 />
    
  2. 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
    
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2024-12-07 07:58:16

试试这个

工作 演示

标记更改

<form ...>
   <input type="radio" name="people" value="student" checked> Student
   <input type="radio" name="people" value="teacher"> Teacher
   <input type="radio" name="people" value="assistant"> Assistant

   <div class="content student">
     <input type="checkbox" name="name" /> Name <br />
     <input type="checkbox" name="Age" /> Age <br />
    <input type="checkbox" name="grade" /> Grade <br />
   </div>

   <div class="content teacher" style="display:none;">
      Teacher content
   </div>

   <div class="content assistant" style="display:none;">
      Assistant content
   </div>
</form>

Js

$(function(){

   $("input[name=people]").click(function(){
      $("div.content").not("."+this.value).hide();
      $("."+this.value).show();
   });

});

Try this

Working demo

Markup change

<form ...>
   <input type="radio" name="people" value="student" checked> Student
   <input type="radio" name="people" value="teacher"> Teacher
   <input type="radio" name="people" value="assistant"> Assistant

   <div class="content student">
     <input type="checkbox" name="name" /> Name <br />
     <input type="checkbox" name="Age" /> Age <br />
    <input type="checkbox" name="grade" /> Grade <br />
   </div>

   <div class="content teacher" style="display:none;">
      Teacher content
   </div>

   <div class="content assistant" style="display:none;">
      Assistant content
   </div>
</form>

Js

$(function(){

   $("input[name=people]").click(function(){
      $("div.content").not("."+this.value).hide();
      $("."+this.value).show();
   });

});
左岸枫 2024-12-07 07:58:16

将所有三个可能的元素放入静态 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

雨夜星沙 2024-12-07 07:58:16

如果我正确理解你的问题...我会创建隐藏的不同 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文