javascript单输入分为两个输入

时间:2017-01-29 16:28:08

标签: javascript

java-script单输入分为两个修复此 前4个字符的输入是用户名和其他密码

<form action="a.php" method="post" id="myform" name="myform">
<label for="textfield">Prepaid Code</label>
<input type="text" name="prepaid" id="prepaid" />
<input type="hidden" name="user" id="user" value="document.getElementById('#prepaid').substring(0,4);"/>
<input type="hidden" name="pass" id="pass" value="document.getElementById('#prepaid').substring(4);"/>
<button onclick="document.getElementById("myform").submit()">SUBMIT</button>

如何仅通过java脚本进行编码

2 个答案:

答案 0 :(得分:0)

我必须同意你所描述的内容听起来像个坏主意。无论您计划什么,都可以以更好的方式完成。使用substring

可以轻松地按照您描述的方式拆分字符串
let user = document.getElementById('#userpass').substring(0,4);
let password = document.getElementById('#userpass').substring(4);

编辑: 看起来你只需要PHP。

$user = substr($_POST['prepaid'], 0, 4);
$pass = substr($_POST['prepaid'], 4);

答案 1 :(得分:0)

我有答案

<script type="text/javascript">


function vowcher(){
var prepaid = document.getElementById('prepaid').value;
var user1 = prepaid.substring(0,4);
var pass1 = prepaid.substring(4);

document.getElementById('user').value =user1;
document.getElementById('pass').value =pass1;
document.getElementById("myform").submit()
}
</script>


<form action="a.php" method="post" id="myform" name="myform">
<label for="textfield">Prepaid Code</label>
<input type="text" name="prepaid" id="prepaid" />
<input type="hidden" name="user" id="user" />
<input type="hidden" name="pass" id="pass" />
<button onclick="vowcher()">SUBMIT</button>
</form>