javascript - 带输入的 onChange 不起作用
问题描述
我的代码遇到了一些麻烦。我根据用户输入即时尝试更改背景颜色。但我无法得到它。我认为这很容易,我只是盲目的。
这是我的 HTML 标记和 jQuery:
<script>
var height;
var width;
$('#height-button').click(function() {
height = $("#height").val();
$("#square").css("height", height);
});
$('#width-button').click(function() {
width = $("#width").val();
$("#square").css("width", width);
});
$(function () {
$("#cpl").change(function(){
$("#square").css("background", $("#cpl").val());
});
})
$(function () {
$('#cp1').colorpicker();
});
</script>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link href="ASSETS/BT-CP/css/bootstrap-colorpicker.css" rel="stylesheet">
<link rel="stylesheet" href="ASSETS/CSS/costum.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="ASSETS/BT-CP/js/bootstrap-colorpicker.js"></script>
</head>
<body>
<div class="container-fluid py-4 col-12">
<div id="square"></div>
</div>
<div class="container py-4 controller-interface">
<ul class="nav nav-tabs nav-justified" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link rounded-0 active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
</li>
<li class="nav-item">
<a class="nav-link rounded-0" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link rounded-0" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
</li>
</ul>
<div class="tab-content py-3 px-3 bg-light border border-top-0" id="pills-tabContent">
<div class="tab-pane show active rounded-bottom" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
<div class="row">
<div class="col-6">
<div class="input-group py-2">
<input type="number" class="form-control" placeholder="Breite" id="width">
<div class="input-group-append">
<button class="btn" id="width-button" type="button">Breitenangabe bestätigen</button>
</div>
</div>
</div>
<div class="col-6">
<div class="input-group py-2">
<input type="number" class="form-control" placeholder="Höhe" id="height">
<div class="input-group-append">
<button class="btn" id="height-button" type="button">Höhenangabe bestätigen</button>
</div>
</div>
</div>
<div class="col-6">
<div class="input-group py-2">
<input type="text" class="form-control" placeholder="Farbe" id="cp1" value="">
</div>
</div>
</div>
</div>
<div class="tab-pane rounded-bottom" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">...</div>
<div class="tab-pane rounded-bottom" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">...</div>
</div>
</div>
我是否必须做一些可以即时更改的事情?如果用户使用颜色选择器触发输入,它会立即改变,那就太好了。
谢谢你的时间!
解决方案
您遇到的问题是因为您的颜色选择器 IDcp1
在您使用的 js 代码中cpl
(l - 作为字母而不是数字)
更改您的代码:
$(function () {
$("#cpl").change(function(){
$("#square").css("background", $("#cpl").val());
});
});
对此:
$(function () {
$("#cp1").change(function(){
$("#square").css("background", $("#cp1").val()); // You can also use $(this).val() here to set the value
});
});
您可以在此处查看代码的工作演示:https ://codepen.io/anon/pen/KerMKg
我无法初始化颜色选择器,但您可以#000
在框中输入值,它会改变背景
推荐阅读
- python - 如何对我的 PyTorch 构建的神经网络进行独特性修剪?
- android - 如何实现搜索视图以在 webview android 应用程序中搜索文本或任何内容?
- elasticsearch - 映射后是否有更好的方法来更新现有字段索引?
- html - 如何使用禁用的 CSS 伪类来设置具有 aria-disabled 属性的元素?
- python - Python合并数据框警告
- forms - 用于多语言实体关系的带有 collectionType 的嵌入式表单
- android - SHARE INTENT Intent.createChooser:如何显示所有已安装的文件浏览器应用程序?
- python - 获取“NotADirectoryError”Python
- nestjs - 如何在nestjs中使用HttpService取消请求
- python - 如何从字典中获取随机选择的值