Javascript 如何使Enter键将其提交到数据库?
当前,当我单击“更改密码”按钮时,此功能可以正常工作,但我希望使用enter键使其正常工作。这是使用Codeigniter,并尝试使用标准html输入类型对其进行更改。但它没有起作用。如果您有任何帮助,我们将不胜感激请使用表单进行包装,浏览器将处理此问题。还可以使用提交按钮类型:Javascript 如何使Enter键将其提交到数据库?,javascript,php,jquery,html,codeigniter,Javascript,Php,Jquery,Html,Codeigniter,当前,当我单击“更改密码”按钮时,此功能可以正常工作,但我希望使用enter键使其正常工作。这是使用Codeigniter,并尝试使用标准html输入类型对其进行更改。但它没有起作用。如果您有任何帮助,我们将不胜感激请使用表单进行包装,浏览器将处理此问题。还可以使用提交按钮类型: public function change_password () { if($this->input->post('submit_btn')){ $this->sessio
public function change_password ()
{
if($this->input->post('submit_btn')){
$this->session->set_flashdata('success','Password Changed Successfully');
redirect('dashboard');
}
// Set up the form
$rules = array(
'newpassword' => array(
'field' => 'newpassword',
'label' => 'New Password',
'rules' => 'trim|required|min_length[6]|xss_clean|check_pass'
),
'conpassword' => array(
'field' => 'conpassword',
'label' => 'Confirm Password',
'rules' => 'trim|required|min_length[6]|matches[newpassword]|xss_clean|check_pass'
),
);
$this->form_validation->set_rules($rules);
$this->form_validation->set_message('required', 'this field is required');
// Process the form
if ($this->form_validation->run() == TRUE) {
$password = $this->input->post("newpassword");
$userid = $this->session->userdata("id");
if($this->user_m->change_password($password, $userid)){
$this->data['message'] = 'password changed successfully';
//$this->data['subview'] = 'home/index';
//$this->load->view('_layout_main_1', $this->data);
}else{
$this->data['message'] = 'password must have at least one uppercase letter and a number';
}
$this->data['refresh'] = true;
}
// Load view
$this->load->view('home/change_password', $this->data);
}
img/close.png“>
修改密码
var code=e.keyCode | | e.which;if(code==13){//Enter keyCode做点击按钮的代码}
按钮的类型是什么,submit
或按钮
?提交按钮正在使用“提交”$(函数(){//twitter引导脚本$(“.submit submit”)。点击(函数(e){var-sdata$('.ajaxForm).serialize();$.ajax({type:“POST”,数据:sdata,url:,success:function(data){$('#baseModal xs.modal content').html(data);}});返回false;})
hi Lamzozoo,我尝试了你的代码,但是提交按钮消失了,回车键仍然不起作用hi Lamzozoo,提交按钮现在出现了,但是回车键仍然不起作用。你能将完整的表单视图添加到问题中吗?不仅仅是按钮本身。我已经编辑了问题,请现在检查一下,我已经包含了你的代码ady.谢谢:)所以如果你已经有了一个按钮,那么你不需要在按钮周围使用表单包装器,只要按钮就可以了!编辑了我的答案!
public function change_password ()
{
if($this->input->post('submit_btn')){
$this->session->set_flashdata('success','Password Changed Successfully');
redirect('dashboard');
}
// Set up the form
$rules = array(
'newpassword' => array(
'field' => 'newpassword',
'label' => 'New Password',
'rules' => 'trim|required|min_length[6]|xss_clean|check_pass'
),
'conpassword' => array(
'field' => 'conpassword',
'label' => 'Confirm Password',
'rules' => 'trim|required|min_length[6]|matches[newpassword]|xss_clean|check_pass'
),
);
$this->form_validation->set_rules($rules);
$this->form_validation->set_message('required', 'this field is required');
// Process the form
if ($this->form_validation->run() == TRUE) {
$password = $this->input->post("newpassword");
$userid = $this->session->userdata("id");
if($this->user_m->change_password($password, $userid)){
$this->data['message'] = 'password changed successfully';
//$this->data['subview'] = 'home/index';
//$this->load->view('_layout_main_1', $this->data);
}else{
$this->data['message'] = 'password must have at least one uppercase letter and a number';
}
$this->data['refresh'] = true;
}
// Load view
$this->load->view('home/change_password', $this->data);
}
<?php echo form_open('',array('class'=>'ajaxForm')); ?>
<div class="modal-header">
<a class="modal-close-med" data-dismiss="modal" aria-hidden="true"><img src="<?php echo base_url(); ?>img/close.png"></a>
<h3 class="modal-title no-margin margin-bottom-5p-min">Change Password</h3>
</div>
<div class="modal-body">
<?php if(isset($message))echo '<span class="text-success txt-upper" style="margin-left:2rem;">'. $message .'</span>';?>
<fieldset class="table ">
<div class="form-group">
<?php $class = form_error('newpassword')?"input-error":"" ?>
<div class="col-md-12" style="margin: 10px 0"><?php echo form_password('newpassword','','class="form-control margin-both-0 '. $class.'" id="newpassword" placeholder="New Password" autocomplete="off"'); ?><?php echo form_error('newpassword'); ?></div>
</div>
<div class="form-group">
<?php $class = form_error('conpassword')?"input-error":"" ?>
<div class="col-md-12 " style="margin: 10px 0"><?php echo form_password('conpassword','','class="form-control margin-both-0 '. $class.'" id="conpassword" placeholder="Confirm Password" autocomplete="off"'); ?><?php echo form_error('conpassword'); ?></div>
</div>
</fieldset>
</div>
<div class="modal-footer">
<?php echo form_submit('submit_btn', 'Change Password', 'class="submit btn btn-success margin-left-4p pad-1-rem margin-bottom-10"'); ?>
</div>
<?php echo form_close();?>
<script>
$(function() {
$('.ajaxForm').on('submit', function(e) {
// ex.: $.post(...
console.log(e);
e.preventDefault();
})
})
</script>