简体   繁体   English

如何确保每个输入字段的值大于先前输入的值?

[英]How to make sure every input field has greater value than the value of previous input?

How to make sure that every field has greater value than the value of previous input? 如何确保每个字段的值都大于先前输入的值? If condition is true, then I can submit a form. 如果条件为真,那么我可以提交表格。

 $('#add').on('click', function() { $('#box').append('<div id="p1"><input required type="number" min="1" max="120" name="val" ></div>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="add" href="javascript:void(0);">Add </a> <form> <div id="box"></div> <input type="submit" value="Submit"> </form> 

You need to loop through all the input s, keeping the value of the previous one to compare it. 您需要遍历所有input s,保持前一个input的值以进行比较。 Keep in mind, your current "add input" code will give all the inputs the same name, which will make it problematic to use on your action page. 请记住,您当前的“添加输入”代码将为所有输入提供相同的名称,这将使您在操作页面上使用时出现问题。 You can use an array for that. 你可以使用一个数组。

 $("#add").on("click", function() { $("#box").append('<div id="p1"><input required type="number" min="1" max="120" name="val[]" ></div>'); }); $("form").submit(function(e) { return higherThanBefore(); //send depending on validation }); function higherThanBefore() { var lastValue = null; var valid = true; $("input[name^=val]").each(function() { var val = $(this).val(); if (lastValue !== null && lastValue >= val) { // not higher than before, not valid valid = false; } lastValue = val; }); return valid; // if we got here, it's valid } 
 <a id="add" href="javascript:void(0);">Add </a> <form action="test"> <div id="box"></div> <input type="submit" value="Submit"> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

One line added, one line changed. 一行添加,一行更改。 Simply get the last input's value, and use that as the min value for the new input. 只需获取最后一个输入的值,并将其用作新输入的最小值。

 $('#add').on('click', function() { // get the current last input, save its value. // This will be used as the min value for the new el var newMin = $("#box").find(".p1 input").last().val() || 1; // Append the new div, but set the min value to the // value we just saved. $('#box').append('<div class="p1"><input required type="number" min="'+newMin+'" max="120" name="val" ></div>'); $(".p1 input").on("keyup mouseup", function(){ var triggeringEl = $(this); if (triggeringEl.val() >= triggeringEl.attr("min") ) { triggeringEl.removeClass("error"); } triggeringEl.parent().nextAll(".p1").children("input").each(function(){ if($(this).attr("min") < triggeringEl.val() ) $(this).attr("min", triggeringEl.val() ); if ($(this).val() < $(this).attr("min")){ $(this).addClass("error"); } else { $(this).removeClass("error"); } }) }) }); 
 .error { border: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="add" href="javascript:void(0);">Add </a> <form> <div id="box"></div> <input type="submit" value="Submit"> </form> 

So I made changes, to reflect the comments (great catch, by the way), but there is a challenge here. 所以我做了改动,以反映评论(顺便提一下),但这里有一个挑战。 If I set the minimum value when the current el's value changes, works great. 如果我在当前el的值改变时设置最小值,那么效果很好。 But I can't assume that the current el is the highest value in the collection, so if the current el is being decremented, I haven't figured the logic to decrement all subsequent minimums. 但我不能假设当前el是集合中的最高值,所以如果当前el正在递减,我还没有想出减少所有后续最小值的逻辑。 Sigh... 叹...

At any rate, the section that creates the new input and sets the minimum remains the same. 无论如何,创建新输入并设置最小值的部分保持不变。 Then I had to add a listener to handle changes to the input. 然后我不得不添加一个监听器来处理对输入的更改。 If the input is changed, by either keyboard or mouse, all subsequent minimums (minima?) are checked against this value. 如果通过键盘或鼠标更改输入,则会根据此值检查所有后续最小值(最小值?)。 Those that are lower are set to this value, and then all elements are checked, minimum vs. value, and an error signal is set if needed. 将较低的值设置为此值,然后检查所有元素,最小值与值,并根据需要设置错误信号。 Still needs work, as I can't figure how to handle decrementing a value, but it's a start. 仍然需要工作,因为我无法计算如何处理递减值,但它是一个开始。

You can use .filter() : for each input field you can test if the next one has a value greater then the current one. 您可以使用.filter() :对于每个输入字段,您可以测试下一个输入字段是否具有大于当前值的值。

 $('#add').on('click', function() { var idx = $('#box').find('div[id^=p]').length; $('#box').append('<div id="p' + idx + '"><input required type="number" min="1" max="120" name="val' + idx + '" ></div>'); }); $('form').on('submit', function(e) { var cachedValues = $('form [type=number]'); var noOrderRespected = cachedValues.filter(function(idx, ele) { var nvalue = cachedValues.eq(idx + 1).val(); return (+ele.value < (+nvalue||+ele.value+1)) ? false : true; }).length; console.log('noOrderRespected: ' + noOrderRespected); if (noOrderRespected > 0) { e.preventDefault(); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="add" href="javascript:void(0);">Add </a> <form> <div id="box"></div> <input type="submit" value="Submit"> </form> 

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

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