简体   繁体   English

JS - 防止追加多次添加

[英]JS - Prevent append to be added multiple times

I have a form that has a mobile field.我有一个带有移动字段的表单。 On submit button I put an event to add a value to the mobile field (it adds the country region code automatically which is a fixed value of "11"), so when the user clicks on Submit, the JS adds the "11" value to the mobile so the this field goes to the data base like this "1155555555" (the user just typed "55555555").在提交按钮上,我放置了一个事件来向移动字段添加一个值(它会自动添加国家/地区代码,这是一个固定值“11”),因此当用户单击“提交”时,JS 会添加“11”值到手机,所以这个字段会像这样“1155555555”(用户刚刚输入“55555555”)进入数据库。

Ok, the problem is that if the user left an empty field (all fields are required), and clicks on Submit, the form won´t be sent but it will add the value "11" to the mobile field no matter what, and when the user fills up the empty field and click on Submit for the second time, it will add AGAIN the value "11", so the mobile goes like " 1111 55555555", and so on and so forth.好的,问题是如果用户留下一个空字段(所有字段都是必需的),然后点击提交,表单将不会被发送,但无论如何它都会向移动字段添加值“11”,并且当用户填写空白字段并第二次单击提交时,它会再次添加值“11”,因此移动设备会显示“ 1111 55555555”,依此类推。

Basically, what I need is to prevent this function from happening multiple times.基本上,我需要的是防止此功能多次发生。 It has to happen only once.它必须只发生一次。 How do I achieve this using JS?我如何使用 JS 实现这一目标?

HTML: HTML:

   <input id="mobile" name="MOBILE" type="tel"><input type="number" value="11" id="front" class="hide">
 <button type="submit" onclick="append11()">SUBMIT</button>

JS: JS:

    function append11(){
    var mobilenumber = document.getElementById("mobile");
  var front = document.getElementById("front").value;
  mobilenumber.value=front+mobilenumber.value;
  alert(mobilevalue.value);
}

I think you should heed the comment responses to your original question.我认为您应该注意对原始问题的评论回复。 Your approach has some risks.你的方法有一些风险。

But I'll assume you're a beginner who's just trying to learn how to do something like what you're asking about, so the javascript below applies a few principles you might consider.但我假设您是一个初学者,只是想学习如何做您所要求的事情,因此下面的 javascript 应用了一些您可能会考虑的原则。

 function isNumberValid9(num) { console.log(num, num.length); //check string length and pattern //this could be combined into a single regex, eg: mobileValue.match("^[0-9]{9}$") var isValid9 = num.length === 9 && num.match("^[0-9]+$"); console.log(isValid9); //display the value about to be returned return isValid9; } /* Conditionally prepend "11" */ function maybeAppend11() { var mobilenumber = document.getElementById("mobile"); var mobileValue = mobilenumber.value; //only prepend "11" if the number matches your expected pattern and length if (isNumberValid9(mobileValue)) { var front = document.getElementById("front").value; mobilenumber.value = front + mobileValue; } alert(mobilenumber.value); }
 <input id="mobile" name="MOBILE" type="tel" value="555555555"><input type="number" value="11" id="front" class="hide"> <button type="submit" onclick="maybeAppend11()">SUBMIT</button>

Why you don't append the 11 in the function?为什么不在函数中附加 11?

Like:喜欢:

function append11(){
  var mobilenumber = document.getElementById("mobile");
  mobilenumber.value="11"+mobilenumber.value;
  alert(mobilevalue.value);
}

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

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