簡體   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