![](/img/trans.png)
[英]JQuery - How do I get the value that was set in an input field's value attribute?
[英]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.