How to know which form id a function is being sent from?

Asked

Viewed 1,343 times

2

How do I know which form id is sent from which function? The function is buscar_cidades() and is present in 2 forms Pessoa Física and Pessoa Jurídica.

How to know in jQuery which form(id) is being sent the function?

<form action="" id="pessoa_fisica">
    <select name="estado" onchange="buscar_cidades()">
       ...
    </select>
</form>

<form action="" id="pessoa_juridica">
    <select name="estado" onchange="buscar_cidades()">
       ...
    </select>
</form>

Updating

Knowing which id is sending the function, how to capture the select of that id knowing that select has class estado?

  var formid = $(e).closest("form").attr("id");
  var estado = ??????????

2 answers

5


In Javascript you can pass this in your role that is called at the event change so that within it you can know which element used it.

With jQuery you can then convert to an object, go up to the form and collect the id:

Example in Jsfiddle

function buscar_cidades(e) {
    var formId = $(e).closest("form").attr("id");
    alert(formId);
}

For a Javascript-only solution, you can:

function buscar_cidades(e) {
    var formId = e.form.getAttribute("id");
    alert(formId);
}
  • You helped me perfectly, you can just give me one more push on the part Updating of the question??

  • The select is the $(e) jQuery when inside the function. If for example you want the selected value: alert($(e).find('option:selected').val());

0

<form id="Formulário" name="Formulário1" method="post" action= "ExercicioPratico4.php">

    <p><b><font size="10px">FICHA DE SÓCIO</font></b></p>
    <p>Nome:
        <label for="nome"></label>
        <input type="text" name="nome" id="nome" size="25" />
    </p>
    <p>Morada:
        <label for="morada"></label>
        <input type="text" name="morada" id="morada" size="30"/>
        Localidade:
        <label for="localidade"></label>
        <input type="text" name="localidade" id="localidade" size="7"/>
    </p>
    <p>Cód. Postal:
        <label for="postal"></label>
        <input type="text" name="postal" size="2" maxlength="4" id="postal"> - <input type="text" name="cep2" size="2" maxlength="3">
        Data de nascimento:
        <label for="nascimento"></label>
        <input type="text" name="dia" size="1" maxlength="2" value="dd" id="nascimento"> /
        <input type="text" name="mes" size="1" maxlength="2" value="mm" id="nascimento"> /
        <input type="text" name="ano" size="2" maxlength="4" value="aaaa" id="nascimento">
        Nacionalidade:
        <label for="nacionalidade"></label>
        <input type="text" name="nacionalidade" id="nacionalidade" size="15"/>
    </p>
    <p>Tel:
        <label for="tel"></label>
        <input type="text" name="tel" id="tel" size="7" maxlength="9"/>
        Telemóvel:
        <label for="telemovel"></label>
        <input type="text" name="telemovel" id="telemovel" size="7" maxlength="9"/> 
        Email:
        <label for="email"></label>
        <input type="text" name="email" id="email" size="30"/>
    </p>
        <p>Desejo-me inscrever como Sócio, pagando a cota de <input type="checkbox" name="cota">1 Euro <input type="checkbox" name="cota">5 Euros Mensais
        </p>
    <p>Data:
        <label for="data"></label>
        <input type="text" name="dia" size="1" maxlength="2"> /
        <input type="text" name="mes" size="1" maxlength="2"> /
        <input type="text" name="ano" size="2" maxlength="4">
    </p>
            <p>
        <input type="submit" name="Enviar" id="Enviar" value="Enviar" />
        </p>
</form> 






echo "Localidade: $localidade<br>";

$postal = $_POST["postal"];
echo "Cód. Postal: $postal <br>";

$nascimento = $_POST["dia"];
echo "Data de nascimento: $nascimento/";

$nascimento = $_POST["mes"];
echo "$nascimento/";

$nascimento = $_POST["ano"];
echo "$nascimento<br>";

$nacionalidade = $_POST["nacionalidade"];
echo "Nacionalidade: $nacionalidade<br>";

$tel = $_POST["tel"];
echo "Tel: $tel<br>";

$telemovel = $_POST["telemovel"];
echo "Telemóvel: $telemovel<br>";

$email = $_POST["email"];
echo "Email: $email<br>";

$cota = $_POST["cota"];
echo "Cota: $cota<br>";

$data = $_POST["dia"];
echo "Data: $data/";

$data = $_POST["mes"];
echo "$data/";

$data = $_POST["ano"];
echo "$data<br>";
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.