繁体   English   中英

单击链接时输入中的文本

[英]Text in an input when clicked on a link

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

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' }; 

Javascript真的不符合您的想法。 我建议您一些教程。 这段代码是正确的:

<a href="#contato" target="_self" onclick="writeText()" class="button">botão</a>
<form method="post" action="" class="" >
<input type="text" name="Button that was clicked" id="text">
</form>

<script>
function writeText() {
    document.getElementById('text').value = 'text exemple';
}
</script>

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

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

您好,首先,我认为您应该遵循jQuery的一些教程,在此处可以找到示例: W3SCHOOL JQUERY

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

  1. 将jQuery CDN添加到您的** HTML中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  1. 之后,您必须选择“点击链接” ,如下所示:

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

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

    function(){var id = $(this).attr(“ id”);

      if(id === "5" ){ $("#inputResult").val('text exemple'); }; } 

最后,它将为您提供以下演示:

 $("a").on("click",function(){ var id=$(this).attr("id"); if(id === "5" ){ $("#inputResult").val('text exemple'); }; }); 
 <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> 

PS:请注意,条件是if(id === "5")而不是if(id === 5)我们没有将id与数字进行比较,因为我们要获取属性值,并且该值是一个string 所以我们必须将id转换为数字或将其与string进行比较,这就是我所做的

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM