gpt4 book ai didi

javascript - 仅选择其中包含数字的值属性

转载 作者:行者123 更新时间:2023-12-01 02:25:33 26 4
gpt4 key购买 nike

我有很多输入标签,分别带有类型文本数字。我想将所有具有数字值的输入类型文本替换为输入类型数字,但不知道如何仅获取数字值。我正在使用下面的代码来做到这一点。

$( "[value*='']").attr('type','number');

最佳答案

您可以尝试.filter并使用isNaN检查值是否为数字

$(document).ready(function() {
$("input[type='text']").filter(function() {
return !isNaN( $(this).val() ) && $(this).val().trim() !== "";
}).attr("type", "number");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="Javascript">
<input type="text" value="Apple">
<input type="text" value="1">
<input type="text" value="3">
<input type="text" value="1000">
<input type="text" value="">

关于javascript - 仅选择其中包含数字的值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com