更改选择选项时提交整个表单而不重新加载页面

时间:2017-11-08 11:41:33

标签: php jquery forms

当我更改select选项并将其保存到mysql而不重新加载页面时,我需要提交整个表单。

我有这个代码,但只有POST选择选项值,我也需要在表单中POST隐藏值。

<form class="form-horizontal" method="POST" action='#' name="dateForm">
  <input type='hidden' name='cond_acao' class='form-control' value="edit_seccao_formando">
  <input type="hidden" name="id_formando" value="<?=$linha_formandos[id_formando];?>">
  <select name="id_seccoes" class="form-control" onchange="return postSelection">
       <option selected="selected" value="1">car</option>
       <option value="2">boat</option>
       <option value="3">plane</option>     
    </select>
  <div id='response_seccoes'></div>
  <script>
    function postSelection(selectObject) {
      var id_seccoes = window.dateForm.id_seccoes.value = selectObject.options[selectObject.selectedIndex].value;
      var dataString = "id_seccoes=" + id_seccoes;
      $.ajax({
        type: "post",
        url: "url.php",
        data: dataString,
        success: function(response) {
$('#response_seccoes').html("ok").fadeIn(100).delay(2000).fadeOut("slow");
          //$("#list").html(response);
        }
      });
      return false;
    };
  </script>
</form>

可以序列化而不是指定字符串吗?

有人能帮帮我吗?谢谢

2 个答案:

答案 0 :(得分:0)

您正在使用Jquery,我建议您使用jquery函数监视选择按钮以进行更改,例如

$("id_seccoes").change(function(){
    //call your post method here.
});

答案 1 :(得分:0)

用此替换您的代码。

<form class="form-horizontal" method="POST" action='#' name="dateForm">
<input type='hidden' name='cond_acao' class='form-control' value="edit_seccao_formando">
<input type="hidden" name="id_formando" value="<?=$linha_formandos[id_formando];?>">                           
<select name="id_seccoes" class="form-control">
<option value="1">car</option>
<option value="2">boat</option>
 <option value="3">plane</option>      
</select>
</form> 
<div id='response_seccoes'></div>

<script>
$(document).ready(function(){
    $('[name="id_seccoes"]').change(function(){
        $.post('url.php',$('[name="dateForm"]').serialize(),function(response){
            $('#response_seccoes').html("ok").fadeIn(100).delay(2000).fadeOut("slow");
        });
    });
});
</script>