表单操作 - 使用jQuery进行更改

时间:2017-09-15 21:47:16

标签: jquery html

<form name="selEstadoRepresentante" id="form" method="post" action="/representantes/?uf=">
  <select class="selectpicker" name="state" id="state">
    <option value="">Selecione</option>
    <option value="AC">Acre</option>
    <option value="AL">Alagoas</option>
    <option value="AP">Amapá</option>
    <option value="AM">Amazonas</option> 
    <option value="BA">Bahia</option>
    <option value="CE">Ceará</option>
    <option value="DF">Distrito Federal</option>
    <option value="ES">Espírito Santo</option>
    <option value="GO">Goiás</option>
    <option value="MA">Maranhão</option>
    <option value="MT">Mato Grosso</option>
    <option value="MS">Mato Grosso do Sul</option>
    <option value="MG">Minas Gerais</option>
    <option value="PA">Pará</option>
    <option value="PB">Paraíba</option>
    <option value="PR">Paraná</option>
    <option value="PE">Pernambuco</option>
    <option value="PI">Piauí</option>
    <option value="RJ">Rio de Janeiro</option>
    <option value="RN">Rio Grande do Norte</option>
    <option value="RS">Rio Grande do Sul</option>
    <option value="RO">Rondônia</option>
    <option value="RR">Roraima</option>
    <option value="SC">Santa Catarina</option>
    <option value="SP">São Paulo</option>
    <option value="SE">Sergipe</option>
    <option value="TO">Tocantins</option>
  </select>
  <input class="btBuscar" name="btBuscar" value="BUSCAR" type="submit" title="Buscar">
</form>

步骤:

  1. 选择:其中一个选项
  2. 选项值:例如: AM
  3. 提交
  4. <form name="selEstadoRepresentante" id="form" method="post" action="/representantes/?uf=AM">
  5. 我该怎么做?

    我认为jQuery解决了这个问题但我不知道如何做到这一点。

3 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$('#state').change(function() {
  var action = '/representantes/?uf=' + $(this).find(":selected").val();
  $('#form').attr('action', action);
  alert($('#form').attr('action'));
});
&#13;
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
<form name="selEstadoRepresentante" id="form" method="post" action="/representantes/?uf=">
  <select class="selectpicker" name="state" id="state">
    <option value="">Selecione</option>
    <option value="AC">Acre</option>
    <option value="AL">Alagoas</option>
    <option value="AP">Amapá</option>
    <option value="AM">Amazonas</option> 
    <option value="BA">Bahia</option>
    <option value="CE">Ceará</option>
    <option value="DF">Distrito Federal</option>
    <option value="ES">Espírito Santo</option>
    <option value="GO">Goiás</option>
    <option value="MA">Maranhão</option>
    <option value="MT">Mato Grosso</option>
    <option value="MS">Mato Grosso do Sul</option>
    <option value="MG">Minas Gerais</option>
    <option value="PA">Pará</option>
    <option value="PB">Paraíba</option>
    <option value="PR">Paraná</option>
    <option value="PE">Pernambuco</option>
    <option value="PI">Piauí</option>
    <option value="RJ">Rio de Janeiro</option>
    <option value="RN">Rio Grande do Norte</option>
    <option value="RS">Rio Grande do Sul</option>
    <option value="RO">Rondônia</option>
    <option value="RR">Roraima</option>
    <option value="SC">Santa Catarina</option>
    <option value="SP">São Paulo</option>
    <option value="SE">Sergipe</option>
    <option value="TO">Tocantins</option>
  </select>
  <input class="btBuscar" name="btBuscar" value="BUSCAR" type="submit" title="Buscar">
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

jsFiddle:https://jsfiddle.net/fkpnvecq/1/

Jquery代码

$(function()
{
  $('.selectpicker').on('change', function()
  {
    if($(this).val())
    {
        $('#form').submit();
    }
  });
});

在下拉列表selectpicker上进行更改时,请检查其是否具有值$(this).val(),如果它具有值,则选择其ID为#form的表单,然后运行{ {1}}方法。

如果您只想让它适用于特定值,您只需使用if状态submit

答案 2 :(得分:0)

如果你想用PHP实现同样的目的,为什么不使用Method作为GET并将字段名称更改为uf,就像这样?

    <form name="selEstadoRepresentante" id="form" method="get" action="/representantes">
      <select class="selectpicker" name="uf" id="state">
        <option value="">Selecione</option>
        <option value="AC">Acre</option>
        <option value="AL">Alagoas</option>
        <option value="AP">Amapá</option>
        <option value="AM">Amazonas</option> 
        <option value="BA">Bahia</option>
        <option value="CE">Ceará</option>
        <option value="DF">Distrito Federal</option>
        <option value="ES">Espírito Santo</option>
        <option value="GO">Goiás</option>
        <option value="MA">Maranhão</option>
        <option value="MT">Mato Grosso</option>
        <option value="MS">Mato Grosso do Sul</option>
        <option value="MG">Minas Gerais</option>
        <option value="PA">Pará</option>
        <option value="PB">Paraíba</option>
        <option value="PR">Paraná</option>
        <option value="PE">Pernambuco</option>
        <option value="PI">Piauí</option>
        <option value="RJ">Rio de Janeiro</option>
        <option value="RN">Rio Grande do Norte</option>
        <option value="RS">Rio Grande do Sul</option>
        <option value="RO">Rondônia</option>
        <option value="RR">Roraima</option>
        <option value="SC">Santa Catarina</option>
        <option value="SP">São Paulo</option>
        <option value="SE">Sergipe</option>
        <option value="TO">Tocantins</option>
      </select>
      <input class="btBuscar" name="btBuscar" value="BUSCAR" type="submit" title="Buscar">
    </form>

如果你必须专门使用jQuery,那么你可以这样做:

$('#state').change(function() {
        var formaction = '/representantes/?uf=' + 
$(this).find(":selected").val();
        $('#form').attr('action', formaction);
        $('#form').submit();
    });