簡體   English   中英

將值設置為aria控制輸入

[英]Set value to an aria-controls input

我想設置一個輸入的aria控制值。 但是我無法使用傳統的jQuery方法來做到這一點。

我的代碼是這樣的:

 function showMessage() { var message = jQuery("#textToDisplay").val(); $("#example").text(message); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="textToDisplay" /> <input type="button" value="Add message" onClick="showMessage()" /> <input aria-controls="example" type="text"> 

如何設置值?

像這樣更改選擇器。您的選擇器以id為目標而不是aria-controls因此請與attr選擇器一起使用。輸入還沒有text()屬性,因此請用val()更改

 function showMessage() { var message = jQuery("#textToDisplay").val(); $("input[aria-controls=example]").val(message); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="textToDisplay" /> <input type="button" value="Add message" onClick="showMessage()" /> <input aria-controls="example" type="text"> 

對於下面的Terry評論,請使用addeventlistener而不是內聯單擊功能。

 $('#click').click(function() { var message = jQuery("#textToDisplay").val(); $("input[aria-controls=example]").val(message); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="textToDisplay" /> <input type="button" id="click" value="Add message"> <input aria-controls="example" type="text"> 

使用attr選擇器

選擇具有指定屬性且其值與特定值完全相等的元素。

$( "input[aria-controls='example']").val(message);

您可以使用

$("#myId").attr("aria-controls", myValue);

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM