0

有谁知道我可以实现以下目标的方法?

当用户在输入框中输入 1 时,会弹出一个警报,但我希望用户能够确认新值并自动用最小值填充输入字段或取消并且该字段保持空白。

到目前为止,我有以下代码:

function Form_Validator(theForm)
{
    var err = false;
    var field;
    var msg = 'Please correct the following to continue...\n';
    var alpha = /\w/;
    var numeric = /[^0-9^\s]/;
    var emailvalidator = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

    var teststr = '';


        if(theForm.prod_quantity.value == '1'){
        msg += '\nThe minimum order amount is 2.';
        if (!err){
            field = theForm.prod_quantity;
            err = true;
        }
    }

        if (!err){
        return true;
    }
    else{
        alert(msg);
        field.focus();
        return false;
    }
}


<b>Quantity:</b> <input type="text" class="prod_quantity" name="prod_quantity"  />
<input type="submit" value="" class="add-basket-btn"/>

我设法让一个警告框出现,但是我不知道如何让它自动填充,以便在他们点击确定时值变为 2。我希望这是有道理的。

4

2 回答 2

0

使用简单的confirm()对话框而不是alert()条件,并在输入框中设置所需的值:

if (confirm (msg)) {  // ask for confirmation
   // set the new value 
   theForm.prod_quantity.value = 2;
} else {
   // leave the box blank
   theForm.prod_quantity.value = '';
}
​
于 2012-09-05T16:37:49.060 回答
0
// ...
var orderAmount = confirm(msg);
if ( orderAmount ) theForm.prod_quantity.value = "2";
于 2012-09-05T16:38:35.763 回答