Add hidden value to imput form (probably JS) [closed]

邮差的信 提交于 2021-01-29 10:38:14

问题


So, I have this html form which the user fills up with his/her info. There is the mobile number field.

I´d like to add the country code automatically, but hidden from the user (since it´s going to be just a local - geographic - marketing action, it´s a fixed code), so I can receive the mobile number with the country code added directly to the database.

All I need is that the form (or JS) adds the value "11" to the beginning of the mobile number without the user noticing it. The user writes "321-456789" and I will receive "11321456789".

How to code that? Thanks!


回答1:


You can use JS to prepend (kudos to Stephen P's amazing vocabulary) "11" to the value of the input field like so:

function append11(){
    var mobilenumber = document.getElementById("phonenumber");
  var front = document.getElementById("front").value;
  mobilenumber.value=front+mobilenumber.value;

}
<input type="number" id="phonenumber"><input type="number" value="11" id="front" hidden><button onclick="append11()">
Append 11
</button>

In the example above, whatever you input, after clicking the 'Append 11' button, will add "11" to the front of the value. You can configure this to submit a form like so:

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

}
<form action="something" method="GET">
<input name="phonenumber" type="number" id="phonenumber"><input type="number" value="11" id="front" hidden><button onclick="append11()">
Submit
</button>

</form>


来源:https://stackoverflow.com/questions/64234695/add-hidden-value-to-imput-form-probably-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!