简体   繁体   English

Javascript/JQuery 多输入,获取输入位置

[英]Javascript/JQuery multiple input, get input position

I have a form with multiple input with same name.我有一个具有多个同名输入的表单。 User will be able to add/remove the input line dynamically.用户将能够动态添加/删除输入行。 eg.例如。

<div><select name="Car[]"></select> <input name="Qty[]"></div>
<button> Add another line</button>

With user interaction, there's a possibility it will become:通过用户交互,它有可能变成:

<div><select name="Car[]"></select> <input name="Qty[]"></div>
<div><select name="Car[]"></select> <input name="Qty[]"></div>
<div><select name="Car[]"></select> <input name="Qty[]"></div>
.......
<button> Add another line</button>

The add button just make a clone of the parent () and append it below.添加按钮只是复制父 () 并将其附加在下面。

I want to enforce eg Honda Civic, the only allowed qty is less than 9. Toyota Prius qty is less than 17...我想强制执行例如本田思域,唯一允许的数量少于 9。丰田普锐斯数量少于 17...

The enforcement will be using on jquery.change() function.强制将在 jquery.change() 函数上使用。 $(input[name="Qty"]).change) $(input[name="Qty"]).change)

$( input[name="Qty"] ).change(function() {
   if(***value car*** == "Honda Civic" && $(this).val() > 9) $(this).val(0);
   else if(***value car*** == "Toyota Prius" && $(this).val() > 17) $(this).val(0);
   else if(***value car*** == "Maserati Ghibli" && $(this).val() > 3) $(this).val(0); 
});

How can I get the input quantity position, and get the value of input car?如何获取输入数量位置,并获取输入汽车的价值?

Can't you create an object which stores car names to limits?您不能创建一个将汽车名称存储到限制的对象吗? For example:例如:

var carLimits = {
    'Honda Civic': '8',
    'Toyota Prius': '16'
};

You could then just count how many 'things' the user has added.然后,您可以计算用户添加了多少“东西”。

hondaCivicCount++

And then create an if statement to check if the hondaCivicCount is less than than, or equal to the carLimit before adding a 'thing'.然后创建一个 if 语句以在添加“事物”之前检查hondaCivicCount是否小于或等于carLimit

You'd be best of doing some sort of server-side verification though, if whatever you're doing needs to be secure.不过,如果您所做的任何事情都需要安全,那么您最好进行某种服务器端验证。

Try Jquery .size() it will tell how much select or input u have https://api.jquery.com/size/ .试试 Jquery .size() 它会告诉你有多少选择或输入https://api.jquery.com/size/

and get value by using .each() https://api.jquery.com/each/并通过使用 .each() https://api.jquery.com/each/获取价值

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

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