所以我有以下内容:
<input type="text" class="example" id="number" readonly="readonly" value="blue widget" name="widget"></input>
我想使用 javascript/jquery 来设置值“大蓝色小部件”
所以我需要在前面添加或插入,或者其他什么,但我不知道如何仅定位类“example”的值
最佳答案
使用 JQuery,您可以使用 val
更改输入框的值方法。
您可以使用类选择器:
$(".example").val("large blue widget");
或者您可以使用按 ID 选择器:
$("#number").val("large blue widget");
要添加文本,您可以使用:
$(".example").val(function(index, text) {
return "large " + text;
});
关于javascript - 如何在此表单值之前添加或插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627157/