从确认框中预填充输入文本框

Prefill input text box from confirmation box

本文关键字:输入 文本 填充 确认      更新时间:2023-09-26

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

当用户在输入框中输入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"/>

我已经设法使一个警告框出现,但我不知道如何使它自动填充,以便如果他们点击ok,值变为2。我希望这是有意义的。

在条件中使用简单的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 = '';
}
​
// ...
var orderAmount = confirm(msg);
if ( orderAmount ) theForm.prod_quantity.value = "2";