Pass text from a textbox via parameter in onclick

Asked

Viewed 1,022 times

1

I need to pass as parameter the content that is in a textbox, in the onclick of a button. How can I do this?

This is the HTML code:

<div id="popup">
<table width="300" height="160">
    <tr>

        <td colspan="3" height="20" style="text-align:right"><input type="submit" value="Fechar X" id="FecharModal" onclick="fecharDialog()" /></a></td>
    </tr>
    <tr>
        <td colspan="3" style="text-align: center; font-weight: bold; font-size:15px"><div id="texto"><p></p></div></td>
    </tr>
    <tr>
        <td height="30" style="text-align:center"><input type="submit" value="Sim" id="ValidaSim" onclick="ValidaSim()" /></td>
        <td height="30" style="text-align:center" colspan="2"><input type="submit" value="Não" id="ValidaNao" onclick="ValidaNao()" /></td>
    </tr>
    <tr>
        <td height="30" style="text-align:center; display:none"><input type="text" id="txtValid" /></td>
        <td height="30" style="text-align: center; display: none"><input type="text" id="txtDrop" /></td>
        <td height="30" style="text-align: center; display: none"><input type="text" id="txtMatricula" /></td>
    </tr>
</table>

In Validasim onclick input I need to pass the text that is in textbox txtValid as parameter. Already in the onclick of the Validate input I do not need to pass the text of all textbox below, as parameters.

<td height="30" style="text-align:center; display:none"><input type="text" id="txtValid" /></td>
<td height="30" style="text-align: center; display: none"><input type="text" id="txtDrop" /></td>
<td height="30" style="text-align: center; display: none"><input type="text" id="txtMatricula" /></td>
  • Go where?

  • A warning, it is not a good practice to insert the style tag inside the element, the css standards ask for it to be located in a file . css

1 answer

1

Follow a very simplistic way of doing what you want

var baseUri = 'http://urldoseusite.com/?'

function ValidaSim() {

  var uri = baseUri + "parametro1=" + $('#txtValid').val();
  alert(uri); // Remover esta linha depois quando implementar em seu projeto.
  window.location = uri;

}

function ValidaNao() {

  var uri = baseUri +
    "parametro1=" + $('#txtValid').val() +
    "&parametro2=" + $('#txtDrop').val() +
    "&parametro3=" + $('#txtMatricula').val();
  alert(uri); // Remover esta linha depois quando implementar em seu projeto.
  window.location = uri;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="popup">
  <table width="300" height="160">
    <tr>

      <td colspan="3" height="20" style="text-align:right">
        <input type="submit" value="Fechar X" id="FecharModal" onclick="fecharDialog()" />
      </td>
    </tr>
    <tr>
      <td colspan="3" style="text-align: center; font-weight: bold; font-size:15px">
        <div id="texto">
          <p></p>
        </div>
      </td>
    </tr>
    <tr>
      <td height="30" style="text-align:center">
        <input type="submit" value="Sim" id="ValidaSim" onclick="ValidaSim()" />
      </td>
      <td height="30" style="text-align:center" colspan="2">
        <input type="submit" value="Não" id="ValidaNao" onclick="ValidaNao()" />
      </td>
    </tr>
    <tr>
      <td height="30" style="text-align:center;">
        <input type="text" id="txtValid" value="valor da txt valid" />
      </td>
      <td height="30" style="text-align: center;">
        <input type="text" id="txtDrop" value="valor da txt drop" />
      </td>
      <td height="30" style="text-align: center;">
        <input type="text" id="txtMatricula" value="valor da txt matricula" />
      </td>
    </tr>
  </table>
  <div>

Browser other questions tagged

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