单击链接时输入中的文本

时间:2017-10-24 20:39:35

标签: javascript php jquery

我需要一个解决方案,当点击一个链接时,必须在输入中插入一个值,其中包含被点击的链接的名称。我不知道怎么做。有人能帮我吗?

IT可以是jQuery,JavaScript,PHP ......或多或少遵循这个例子:

<a href="#contato" target="_self" id="5" class="button" style="">botão</a>

<form method="post" action="" class="" >
    <input type="text" name="Button that was clicked" id="" value="id" >
</form>

if (id == 5) {
    id = 'text exemple'
}; 

4 个答案:

答案 0 :(得分:0)

Javascript真的没有按你的想法工作。我推荐你一些教程。这段代码是正确的:

@Query(value = "Select NEW com.company.pathToYourDto.ObjectDTO(count(f), sum(f.valueToPay)) from ObjectEntity f ")
ObjectDTO  getData();

答案 1 :(得分:0)

您必须首先为您的输入提供ID(YOURID)或类,然后jQuery中的解决方案将是:

<script>
jQuery('#5').on("click",function() {
var idbutton = $(this).attr("href");
$('#YOURID').val(idbutton);
});
</script>

答案 2 :(得分:0)

  

您好,首先,我认为您应该遵循一些教程   jQuery,你可以在这里找到例子:W3SCHOOL JQUERY

然后按照以下步骤回答您的问题:

  1. 将jQuery CDN 添加到** HTML
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    1. 之后,您必须选择点击链接,以便它会是这样的:

      $("a").on("click"...

    2. 最后,您必须实现一个函数,该函数将在单击前面选择的链接时执行:

      功能(){     var id = $(this).attr(&#34; id&#34;);

        if(id === "5" ){
              $("#inputResult").val('text exemple');
          };
      }
      
    3. 最后它将为您提供以下演示:

      &#13;
      &#13;
      $("a").on("click",function(){
      var id=$(this).attr("id");
      
        if(id === "5" ){
      		$("#inputResult").val('text exemple');
      	};
        
      });
      &#13;
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <a href="#contato" target="_self" id="5" class="button" style="">botão</a><br>
      <a href="#contato" target="_self" id="6" class="button" style="">botã22222o</a>
      <form method="post" action="" class="" >
      
      <input type="text" name="Button that was clicked" id="inputResult" value="id" >
      
      </form>
      &#13;
      &#13;
      &#13;

        

      PS:请注意,条件为if(id === "5")而不是if(id === 5)我们没有将id与数字进行比较,因为我们正在撤退   属性的值,此值为string。所以我们必须   要么将id转换为数字,要么将其与string进行比较   就是我做的

答案 3 :(得分:0)

ES6解决方案

<a href="#contato" onClick="changeValue()" target="_self" id="5" class="button" style="">botão</a>

<form method="post" action="" class="" >
    <input type="text" name="Button that was clicked" id="textfield" value="id" >
</form>

<script>
    let textVal = 'text example'
    let changeValue = () => {
        document.getElementById("textfield").value = textVal
    }
</script>
相关问题