jQuery当输入是X的间隔时,做一些事情

时间:2021-06-23 01:07:26

I have an input which will have a certain number in it. If they put an incorrect interval number in the input and click the submit button I want it to alert instead of proceeding.

我有一个输入,其中有一定数量。如果他们在输入中输入了错误的间隔号并单击了提交按钮,我希望它提醒而不是继续。

Best shown in Example:

示例中显示的最佳:

Say one enters 7, 8, 9, 10 or 11. I would want an alert("Please enter intervals of 6") show up and the addtocart input not fired. The input value on each page is dynamic, and could be 1, 6, 12, etc..

假设一个进入7,8,9,10或11.我想要一个警报(“请输入6的间隔”)显示并且没有触发addtocart输入。每页上的输入值是动态的,可以是1,6,12等。

<input value="6" type="text" onkeydown="javascript:QtyEnabledAddToCart();" >    
<input class="vCSS_input_addtocart" type="image" src="btn_addtocart.gif" name="btnaddtocart" onclick="return addToCart(this.form, this);">

Perhaps I have to use a change() function?

也许我必须使用change()函数?

2 个解决方案

#1


1  

Use Modulus function like:

使用模数函数,如:

<input class="txt" value="6" type="text" onkeydown="javascript:QtyEnabledAddToCart();" /> 


  if (!(($(".txt").val() % 6 == 0) || ($(".txt").val()==1)))
  {
   alert("Not interval of 6 or value of 1");   
  }

Operators/Modulus.htm">http://www.java2s.com/Tutorial/JavaScript/0040_Operators/Modulus.htm

运营商/ Modulus.htm“> http://www.java2s.com/Tutorial/JavaScript/0040_Operators/Modulus.htm

Working example:

工作范例:

http://jsfiddle.net/Y3SEJ/2/

http://jsfiddle.net/Y3SEJ/2/

#2


0  

if (value<0 || (value!=1 && value%6!=0)) {
   alert("I'm sorry, I can't let you buy that.");
}

#1


1  

Use Modulus function like:

使用模数函数,如:

<input class="txt" value="6" type="text" onkeydown="javascript:QtyEnabledAddToCart();" /> 


  if (!(($(".txt").val() % 6 == 0) || ($(".txt").val()==1)))
  {
   alert("Not interval of 6 or value of 1");   
  }

Operators/Modulus.htm">http://www.java2s.com/Tutorial/JavaScript/0040_Operators/Modulus.htm

运营商/ Modulus.htm“> http://www.java2s.com/Tutorial/JavaScript/0040_Operators/Modulus.htm

Working example:

工作范例:

http://jsfiddle.net/Y3SEJ/2/

http://jsfiddle.net/Y3SEJ/2/

#2


0  

if (value<0 || (value!=1 && value%6!=0)) {
   alert("I'm sorry, I can't let you buy that.");
}