簡體   English   中英

jQuery根據屬性將值設置為輸入字段

[英]Jquery set value to input field based on attribute

我有輸入字段,如:

<input data-checkout="card-number" type="tel" placeholder="card number" autocomplete="off" class="input-control" value="">

如何使用jquery設置此字段的值? 如何獲取data-checkout="card-number"和設置值?

在jquery中,您可以使用以下格式將任何屬性用作選擇器:

$('[your-attr="attr value"]')

回答您的問題,這應該可行:

$('[data-checkout="card-number"]').val("Your val"); 

這取決於您希望通過它訪問什么。 要訪問該元素,可以使用例如:

$('.input-control')  // Finds all elements with this class

要么

$('.input-control[data-checkout=card-number]')  // Finds all elements with this class and this data-checkout value

或其他選擇器,具體取決於您的應用程序。 然后,可以設置值:

$(/* your selector */).val('some value'); // Sets the value to 'some value'

並獲取數據結帳的值:

$(/* your selector */).data('checkout');  // Returns 'card-number'

目前還不清楚您到底在尋找什么,但是希望其中一些可以回答您的問題。

使用jquery可以使用val函數:

$(".input-control").val("Some value")

使用javascript:

document.querySelector(".input-control").value="Some value"

您可以通過以下方式獲取屬性:

$(".input-control").attr("card-number")

那你可以做

$(".input-control").val($(".input-control").attr("card-number"))

希望這可以幫助

您可以使用基本Javascript設置值。

for(i=0; card=document.querySelectorAll("input[data-checkout=card-number]")[i]; i++){
    card.value="sample value";
} 
$('input[data-checkout="card-number"]').val('here') // replace word (here) with the value you want

暫無
暫無

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

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