Svelte使用QuerySelectorAll在我脚本中的形式输入中访问引用的类

发布于 2025-02-11 09:42:42 字数 2145 浏览 1 评论 0原文

我有一个表格,显示一个项目的选项组。用户选择所需数量的选项,然后我验证脚本中所选选项。

表格:

    <form name="optionsform" on:submit|preventDefault={()=>collectoptions(itemid, itemprice, itemname,  itemdescription, variationname, variationprice)}>
    
    {#if option['rules'] == "exactly"}
    {#each option.optiongroupitems as optionitem, index} 
    <li style="list-style : none;">
    <label>
        <input  type="checkbox" class={option.groupheader} bind:group={values.exactly} value={optionitem.name} on:click={()=> Validateexactly(option.rulesnumber.number, option.groupheader, optionitem.name)} >
         {optionitem.name} : {optionitem.price}
    </label>
    </li>
    {/each}
    {/if} 
    <button type="submit" >Add To cart</button>
    </form

在我的脚本中,函数validateExactly():

    function Validateexactly(limit, groupheader, name) { 
    let getexactlygroup = `${groupheader}` 
    console.log("this is logging corrertly the option group name :", getexactlygroup)
    
    exactlylimit = limit
    var checks = document.querySelectorAll(".getexactlygroup"); // how do I reference option.groupheader correctly?  
    checks.forEach((item, index)=>{
    
      console.log("showing empty list array ", item) 
    
    })
    
    for (var i = 0; i < checks.length; i++)
     checks[i].onclick = selectiveCheck;
    function selectiveCheck (event) {  
     var checkedChecks = document.querySelectorAll('.getexactlygroup'[type="checkbox"]:checked');//showing errors that selector is invalid selector. 
     if (checkedChecks.length >= exactlylimit + 1){
       return false;
     }
    }
    
    }

我的问题如何用{option.groupheader}以表单引用类,如何使用queryselectorall在脚本中引用该类?

我需要参考的原因是,因为选项组来自数据库,我无法控制DB,并且每个项目都有一个以上的选项组。因此,我想到按选项组名称对它们进行分组,或者可以引用类似Big String(62B52ED4F7A2ED2A506DB0B1)的组ID,我不确定是否可以使用QuerySelectorAll将其引用。除非还有另一种方法来分组选项并使用不同的方法进行检查。

如果我能弄清楚如何正确参考选项。脚本中的groupheader,我认为我的组织方式很简单。

我的问题是:如何在我的脚本中引用类= {option.groupheader}的类以获取所有类别的输入并访问其长度?

I have a form that display group of options for an item. User select the required number of options and I validate the selected options in the script.

The form :

    <form name="optionsform" on:submit|preventDefault={()=>collectoptions(itemid, itemprice, itemname,  itemdescription, variationname, variationprice)}>
    
    {#if option['rules'] == "exactly"}
    {#each option.optiongroupitems as optionitem, index} 
    <li style="list-style : none;">
    <label>
        <input  type="checkbox" class={option.groupheader} bind:group={values.exactly} value={optionitem.name} on:click={()=> Validateexactly(option.rulesnumber.number, option.groupheader, optionitem.name)} >
         {optionitem.name} : {optionitem.price}
    </label>
    </li>
    {/each}
    {/if} 
    <button type="submit" >Add To cart</button>
    </form

In my script, function validateexactly() :

    function Validateexactly(limit, groupheader, name) { 
    let getexactlygroup = `${groupheader}` 
    console.log("this is logging corrertly the option group name :", getexactlygroup)
    
    exactlylimit = limit
    var checks = document.querySelectorAll(".getexactlygroup"); // how do I reference option.groupheader correctly?  
    checks.forEach((item, index)=>{
    
      console.log("showing empty list array ", item) 
    
    })
    
    for (var i = 0; i < checks.length; i++)
     checks[i].onclick = selectiveCheck;
    function selectiveCheck (event) {  
     var checkedChecks = document.querySelectorAll('.getexactlygroup'[type="checkbox"]:checked');//showing errors that selector is invalid selector. 
     if (checkedChecks.length >= exactlylimit + 1){
       return false;
     }
    }
    
    }

My question how to reference the class with the {option.groupheader} in the form, how to reference that in my script using querySelectorAll?

The reason I need it to reference that is because the option groups is coming from the db and I don't have control over the db and there are more than one option group for each item. So I thought to group them by option group name or I can reference the group id which is like big string (62b52ed4f7a2ed2a506db0b1) and I'm not sure if I can reference that in my script using the querySelectorAll. Unless there is another way to group options and get the checked one using different method.

I see the way I organized it as simple if I can figure out how to reference the option.groupheader in my script correctly.

My question is : How to reference the class={option.groupheader} in my script to get all checked input with that class and access its length?

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

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

发布评论

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

评论(1

花开半夏魅人心 2025-02-18 09:42:42

getExactlyGroup是一个变量,请查看模板文字(模板字符串)

var checks = document.querySelectorAll(`.${getexactlygroup}`);

(如果您看上一个问题的答案,它也在那里)

getexactlygroup is a variable, have a look at Template literals (Template strings)

var checks = document.querySelectorAll(`.${getexactlygroup}`);

(If you have a look at the answer of your last question, it's in there too)

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