简体   繁体   English

使复选框所需的输入文本相乘

[英]make multiplie input text required on checkbox

I have a table with many rows and each row has a checkbox and an input.我有一个有很多行的表,每一行都有一个复选框和一个输入。

I'm tring to make a code snippet that will make the input reqiured when the checkbox on the same row is checked.我正在制作一个代码段,该代码段将在检查同一行上的复选框时将输入重新排列。

Here is what I have tried:这是我尝试过的:

 $(document).ready(function() { $('#prod[1]').click(function() { if ($(this).prop('checked')) { $('#qty_1').attr('required', ''); } else { $('#qty_1').removeAttr('required'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <tr> <td><input type="checkbox" id="prod[1]"><label for="prod[1]">name</label></td> <td><input type="text" id="qty_1" name="qty_1"></td> </tr>

This one isn't working because of the "[]", however, if I remove them, they will work...由于“[]”,这个不起作用,但是,如果我删除它们,它们将起作用......

I dont know how to make it work with the "[]" or for each row (prod[2], prod[3], etc...)我不知道如何使它与“[]”或每一行一起工作(prod[2]、prod[3] 等......)

Thank you!谢谢!

Those brackets have special meaning in css(attribute selector), to use these characters in your jquery selector need to escape them by using double backslashes \\ .这些括号在 css(属性选择器)中具有特殊含义,要在 jquery 选择器中使用这些字符,需要使用双反斜杠\\来转义它们。
Check out the snippet, write down anything inside the textbox before clicking on the check-box and watch the console log result.查看代码段,在文本框中写下任何内容,然后单击复选框并查看控制台日志结果。

 $(document).ready(function() { var prod = $('#prod\\[1\\]'); prod.on('click', function() { if ($(this).prop('checked')) { $('#qty_1').attr('required', ''); } else { $('#qty_1').removeAttr('required'); } console.log($('#qty_1').val()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <tr> <td><input type="checkbox" id="prod[1]"><label for="prod[1]">name</label></td> <td><input type="text" id="qty_1" name="qty_1"></td> </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM