javascript - 在输入表单中插入值
问题描述
选择一张图像时...
<div class="selectable">
<img src="https://glarza.com/imagens/tamanho-33.webp" class="ui-widget-content"value="SIZE-33"width="60"height="60">
<img src="https://glarza.com/imagens/tamanho-34.webp" class="ui-widget-content"value="SIZE-34"width="60"height="60">
</div>
此功能选择大小...
<script>
$(function() {
$(".selectable").selectable({
selected: function() {
var selectedItemList = $("#medida-selecionada-dalista").empty();
$(".selectable img").each(function(index) {
if ($(this).hasClass("ui-selected")) {
selectedItemList.append(
$(this).attr("value") ); } });}});});
</script>
并在 id 内显示选定的大小...
<span id="medida-selecionada-dalista"></span>
<input type="size" class="form-control" id="size" name="size" value="">
解决方案
像这样?
$(function() {
$(".selectable").selectable({
selected: function() {
var selectedItemList = $("#medida-selecionada-dalista").empty();
$(".selectable img").each(function(index) {
if ($(this).hasClass("ui-selected")) {
selectedItemList.append(
$(this).attr("value")
);
/* insert into input */
document.getElementById("size").value = this.getAttribute("value");
}
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="size" class="form-control" id="size" name="size" value="">
<span id="medida-selecionada-dalista"></span>
<div class="selectable">
<img src="https://glarza.com/imagens/tamanho-33.webp" class="ui-widget-content" value="SIZE-33" width="60" height="60">
<img src="https://glarza.com/imagens/tamanho-34.webp" class="ui-widget-content" value="SIZE-34" width="60" height="60">
</div>
PS尽可能避免使用jquery...
推荐阅读
- javascript - 为什么使用字符串作为请求正文时,Axios 发送我的 POST 请求时使用 Content-Type application/x-www-form-urlencoded?
- flutter - Dart/Flutter - 如何在格式化值时避免由 NumberFormat.compactCurrency(locale: "en_IN").format() 方法完成的自动舍入?
- javascript - 当我出于某种原因组合减速器时,我的 auth 减速器没有附加到商店,那是怎么回事?
- python - 使用 OpenCV 将相机坐标中的点云均匀变换到世界坐标
- swift - UITableView 的委托是 Nil
- android - Android GridLayout — 删除一个布局权重会导致所有其他单元格消失
- r - 如何在 R 中编辑单元格 Shapefile?
- arrays - 为什么我得到 org.json.JSONException: Not a original array?
- html - 由于 css 中的 calc(width),React 应用程序中的“npm run build”失败?
- sql - 加入具有多个值的不同列时获得相同的结果