JS模板在QuerySelectorall中的字面词与复选框产生错误:检查条件
这是我今天第三次解决这个问题。每次有人回答我的问题时,我都非常感谢您的帮助,但是问题部分解决了简单的部分。重要的部分仍未产生所需的输出。
我有一个由选项组名称分组的选项的表单。我正在尝试使用模板文字在我的JS代码中获取选项group名称,以检查输入复选框是否已检查。应该很容易,但我在最后一部分无法成功。
我的表格是使用Svelte和HTML
<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;
}
}
}
在很少有人发布答案之后,我使此部分工作了:
let checks = document.querySelectorAll(`.${getexactlygroup}`);
现在,我可以在Console.log中看到所有我的选项组输入。
过去一个小时我尝试的问题是这一行:
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"][checked]);
我尝试了以下内容,并且发现类型不是有效的选择器,输入不是有效的选择器。因此,如何从先前的答案中失败了如何使用模板文字。我读取有关MDN的字面模板,然后读取QuerySelector部分。
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"]:[checked]);
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`input[type="checkbox"]:[checked]);
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"][checked]);
var checkedChecks = document.querySelectorAll( " `.${getexactlygroup}`.input[type="checkbox"]:[checked]");
在MDN中,有人写道: 注意:不属于标准CSS语法一部分的字符必须使用后斜切字符逃脱。由于JavaScript还使用Backsslash逃脱,因此在使用这些字符编写字符串文字时要特别小心。有关更多信息,请参见逃脱特殊字符。
如何使用。$ {getExactlyGroup}
[输入类型复选框]:在JS中检查? 我很沮丧,我认为这不是那么困难,但我无法弄清楚。有JS Ninja带有坚实的JS知识,谁能帮助我?
This is my third time for this issue today. Everytime someone answers my question, I'm very grateful for the help but the issue partially resolved the easy part. The important part still not producing the desired output.
I've a form with options grouped by the option group name. I'm trying to use template literal to get the optiongroup name in my js code to check if the input checkbox checked or not. Should be easy but I'm unable to succeed in this last part.
My form is typical using svelte and html
<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 :
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;
}
}
}
After few kind people posting their answers, I got this part working:
let checks = document.querySelectorAll(`.${getexactlygroup}`);
And now I'm able to see in the console.log all my option group input grouped correctly.
The issue that I've trying for the past hour is this line:
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"][checked]);
I tried the following and I get error that type is not a valid selector, input is not a valid selector. So how, I failed from the previous answers how to use template literal. I read the template literal on MDN, then querySelector section.
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"]:[checked]);
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`input[type="checkbox"]:[checked]);
var checkedChecks = document.querySelectorAll( `.${getexactlygroup}`.input[type="checkbox"][checked]);
var checkedChecks = document.querySelectorAll( " `.${getexactlygroup}`.input[type="checkbox"]:[checked]");
In MDN, someone wrote that :
Note: Characters that are not part of standard CSS syntax must be escaped using a backslash character. Since JavaScript also uses backslash escaping, be especially careful when writing string literals using these characters. See Escaping special characters for more information.
How do I use the .${getexactlygroup}
[input type checkbox] : checked in js?
I'm so frustrated and I don't think it is that difficult but I'm unable to figure it out. Any js ninja out there with rock solid js knowledge who can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您不需要任何查询选择器。
我会构建这样的复选框组件:
You don't need any query selector in svelte in this case.
I would build a checkbox component like this: