当我点击提交表单时,我的javascript函数无效。 有人可以帮忙吗?我认为一切都很好,但由于某些我不知道的原因,它不起作用。
<form action="<?php echo base_url();?>my_data" onsubmit="onSubmit()" method ="POST">
<p class = "my_data_text">Nome</p>
<input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = "<?php echo $this->session->userdata('nome_usuario');?>"></input>
<p class = "my_data_text">Email</p>
<input type="text" class="form-control" style="width:54%;" name = "changed_user_email" value = "<?php echo $this->session->userdata('email_usuario');?>"></input>
<p class = "my_data_text">Digite sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "changed_password"></input>
<p class = "my_data_text">Confirme sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input>
<button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
function onSubmit(){
alert("email inválido");
var result =1;
var checar = document.querySelector("#changed_user_email");
var test = checar.value.indexOf("@");
if (test == -1){
alert("email inválido");
result =0;
}
var checar = document.querySelector("#changed_password");
var passw= /^[A-Za-z]\w{7,14}$/;
if(!checar.value.match(passw)){
alert("password inválido");
result =0;
}
if (result ==0){
return (false);
}
else{
return (true);
}
}
</script>
答案 0 :(得分:1)
试试这个:
<form action="<?php echo base_url();?>my_data" onsubmit="return onSubmit()" method ="POST">
<p class = "my_data_text">Nome</p>
<input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = "<?php echo $this->session->userdata('nome_usuario');?>"></input>
<p class = "my_data_text">Email</p>
<input type="text" class="form-control" style="width:54%;" name = "changed_user_email" id="changed_user_email" value = "<?php echo $this->session->userdata('email_usuario');?>"></input>
<p class = "my_data_text">Digite sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "changed_password" id="changed_password"></input>
<p class = "my_data_text">Confirme sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input>
<button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
function onSubmit(){
var result =1;
var checar = document.getElementById("changed_user_email");
var test = checar.value.indexOf("@");
if (test == -1){
alert("email inválido");
result =0;
}
var checar = document.getElementById("changed_password");
var passw= /^[A-Za-z]\w{7,14}$/;
if(!checar.value.match(passw)){
alert("password inválido");
result =0;
}
if (result ==0){
return false;
}
else{
return true;
}
}
</script>
答案 1 :(得分:0)
我认为是因为你没有使用input type="submit"
而是使用button type="submit"
。
您应该替换提交按钮:
<input type="submit" value="salvar" class="btn btn-primary" id="btn_mydata_submit_incident"/>
或者只更改onSubmit
一个onclick
javascript事件。
答案 2 :(得分:0)
你可以尝试的另一种方式是
$('#button_id').click(function(){
onSubmit(); //call the function,return something true false value from function return and depending on that submit the form
$('#form_id').submit();
});
我希望,它可以帮助你!!!
答案 3 :(得分:0)
使用此代码。完美地为我工作......
<form action="" onsubmit="onSubmit()" method ="POST">
<p class = "my_data_text">Nome</p>
<input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = ""></input>
<p class = "my_data_text">Email</p>
<input type="text" class="form-control" style="width:54%;" name = "changed_user_email" value = ""></input>
<p class = "my_data_text">Digite sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "changed_password"></input>
<p class = "my_data_text">Confirme sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input>
<button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
function onSubmit(){
alert("email inválido");
var result =1;
var checar = document.querySelector("#changed_user_email");
var test = checar.value.indexOf("@");
if (test == -1){
alert("email inválido");
result = 0;
return false;
}
var checar = document.querySelector("#changed_password");
var passw= /^[A-Za-z]\w{7,14}$/;
if(!checar.value.match(passw)){
alert("password inválido");
result =0;
return false;
}
if (result == 0){
return (false);
}
else{
return (true);
}
}
</script>