基于下拉菜单创建必填字段。 [HTML、JAVASCRIPT、JQuery]

发布于 2025-01-12 00:16:12 字数 2158 浏览 1 评论 0原文

我有一个下拉列表 [name="./fixedListForm"],其中有 2 个字段

  1. 外部
  2. 静态内容

下拉值< /a> 选择外部时,将启用具有 2 个字段的多字段。

  1. TabID [文本字段]
  2. Alt 文本 [文本字段]

由于上述两个字段是多字段,我们可以添加 n 个带有名称的字段。例如

  1. 选项卡 ID [文本字段,存储名称=./fixedList/item0/./tabId]
  2. 替代文本 [文本字段,存储名称=./fixedList/item0/./alt] 添加
  3. 选项卡 ID [文本字段,存储名称=./fixedList/item1/./tabId]
  4. 替代文本 [文本字段, ,存储名称=./fixedList/item1/./alt] 添加

外部多字段中的选项卡 ID

等等......它会增加项目 0、项目 1、项目 2… 每个项目都包含 [./tabid 和 ./alt]

在选择静态内容时,将添加另一组具有 2 个字段的多字段

  1. Tab ID [Text Field]
  2. Alt Text [text Field]

由于上述两个字段是多字段,我们可以添加 n 个带有名称的字段。例如

  1. 选项卡 ID [文本字段,使用名称 ./staticContent/item0/./tabId 存储]
  2. 替代文本 [文本字段,使用名称 ./staticContent/item0/./alt 存储] 添加
  3. 选项卡 ID [文本字段,使用名称 ./staticContent/item1/./tabId 存储]
  4. 替代文本 [文本字段, ,使用名称 ./staticContent/item1/./alt 存储] 添加

依此类推...它将增加item0、item1、item2...并且每个item都包含[./tabid和./alt] 静态多字段中的选项卡 ID

现在,这里的要求是,如果我们有的话,我必须强制使用替代文本TAB ID 中的某些值。 意思是

 var tabId = $('.cmp-list__editor').find('input[name *="./tabId"]').val();
    if (tabId == "") {
        $('.cmp-teaser__editor').find('input[name *="./alt"]').attr("aria-required", false);
    }else{$('.cmp-list__editor').find('input[name *="./alt"]').attr("aria-required", true);}

.cmp-list__editor 是父 Div 的类。 我面临的问题是,如果我们有单个值字段,即只有单个 TABID 和 ALT 文本,则上面的代码可以正常工作。

但在这里我们必须创建一个循环,

  1. 如果下拉列表选择了外部值,它将从下拉列表值中进行搜索
  2. ,如果我们在 TAB ID 中有某个值,则逻辑将应用于强制替代文本 [./fixedList/item0/./tabId - ./fixedList/item0/./alt] [./fixedList/item1/./tabId - ./fixedList/item1/./alt]
  3. 如果下拉列表选择了静态值,如果我们在 TAB ID 中有一些值,逻辑将应用强制替代文本 [./staticContent/item0/./tabId - ./staticContent/item0/./alt] [./staticContent/item1/./tabId - ./staticContent/item1/./alt]

I have a dropdown [name="./fixedListForm"] where we have 2 fields

  1. External
  2. Static Content

Dropdown values
On Selection of External, will enable a multifield with 2 fields.

  1. TabID [Text Field]
  2. Alt Text [Text Field]

Since the above two fields are multifield we can add n number of fields with names. e.g.

  1. Tab ID [Text Field, stored with name=./fixedList/item0/./tabId]
  2. Alt Text [text Field, , stored with name=./fixedList/item0/./alt]
    Add
  3. Tab ID [Text Field, stored with name=./fixedList/item1/./tabId]
  4. Alt Text [text Field, , stored with name=./fixedList/item1/./alt]
    Add

Tab ID in External multifield

And so on… it will increase item0, item1, item2…
And each items holds [./tabid and ./alt]

On Selection of Static Content, another set of multifield with 2 fields will be added

  1. Tab ID [Text Field]
  2. Alt Text [text Field]

Since the above two fields are multifield we can add n number of fields with name. e.g.

  1. Tab ID [Text Field, stored with name ./staticContent/item0/./tabId]
  2. Alt Text [text Field, , stored with name ./staticContent/item0/./alt]
    Add
  3. Tab ID [Text Field, stored with name ./staticContent/item1/./tabId]
  4. Alt Text [text Field, , stored with name ./staticContent/item1/./alt]
    Add

And so on… it will increase item0, item1, item2… And each items holds [./tabid and ./alt]
Tab Id in Static Multifield

Now the requirement here is I have to make alt text mandatory if we have some value in TAB ID.
Means something like

 var tabId = $('.cmp-list__editor').find('input[name *="./tabId"]').val();
    if (tabId == "") {
        $('.cmp-teaser__editor').find('input[name *="./alt"]').attr("aria-required", false);
    }else{$('.cmp-list__editor').find('input[name *="./alt"]').attr("aria-required", true);}

where .cmp-list__editor is class of parent Div.
The problem here I am facing is the above code works fine if we have a single value field i.e. only Single TABID and ALT text.

But here we have to create a loop where

  1. It will search from the dropdown value
  2. if dropdown have selected value as external, logic will apply for making alt text mandatory if we have some value in TAB ID
    [./fixedList/item0/./tabId - ./fixedList/item0/./alt]
    [./fixedList/item1/./tabId - ./fixedList/item1/./alt]
  3. if dropdown have selected value as static, logic will apply for making alt text mandatory if we have some value in TAB ID
    [./staticContent/item0/./tabId - ./staticContent/item0/./alt]
    [./staticContent/item1/./tabId - ./staticContent/item1/./alt]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

为人所爱 2025-01-19 00:16:12

如果您检查 multifield 的 HTML,您的每个 multifield 都位于 coral-multifield-item 内。也许您可以获得这个元素(它将返回当时添加到对话框中的所有元素)。获得 this 后,迭代每个元素。

var items = $("coral-multifield-item")

coral-multifield-item

为您的个人字段添加一个独特的类,如下所示

granite:class

添加后,您可以执行以下操作来获取这些字段的值

$( items ).each(function( index ) {
 console.log($( this ).find(".test-class").val());
});

假设测试类是添加到我的多字段中第一个文本字段的类,上面的 JS 将打印 JS 中第一个文本字段的值。对所有字段做类似的逻辑,在jquery中轻松获取。

如果您熟悉 Jquery,您可以调整此逻辑并尝试实现您的要求。否则,请向精通 Jquery 的人寻求帮助。

If you check the HTML for the multifield , each of your multifield is inside coral-multifield-item. May be you can get this element ( which will return all added to the dialog at that point of time) . Once you get this , iterate over each element.

var items = $("coral-multifield-item")

coral-multifield-item

For your individual field add a unique class like below

granite:class

Once that is added , you can do the below to get the value of those fields

$( items ).each(function( index ) {
 console.log($( this ).find(".test-class").val());
});

Assuming test-class is the class added to the first textfields in my multifield, the above JS will print the value of my first text fields in the JS . Do similar logic to all the fields and get it easily in the jquery.

You can tweak this logic and try to achieve your requirement if you are familiar with Jquery. Else take help from someone who is good with Jquery.

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