gpt4 book ai didi

javascript - 不打印控制台日志

转载 作者:行者123 更新时间:2023-12-03 04:42:16 25 4
gpt4 key购买 nike

这里我试图在控制台中显示复选框的值,但它没有。

<input type="checkbox" id="id_price" value="1" onclick="display_img()">Under £200<br>
<input type="checkbox" id="id_price" value="2" > £250-£300<br>
<input type="checkbox" id="id_price" value="3" > £300-£450<br>

Js代码是

function display_img(){
$(document).ready(function(){
$("#id_price").each(function(){
if($(this).is("checked")){
var check=document.getElementById("id_price").value;
console.log(check);
}
});
});

}

最佳答案

您的代码中存在一些错误,例如:

  • 您使用了多个 ID。每个元素的 id 应该是唯一的
  • 您仅在其中一个元素上使用了 onclick=""
  • 应该是$(this).is(":checked")

下面是编辑后的 ​​JavaScript 和 HTML。

function display_img() {
$(".id_price").each(function() {
if ($(this).is(":checked")) {
var check = $(this).val();
console.log(check);
}
});
};

$(".id_price").change(display_img);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="id_price" value="1">Under £200
<br>
<input type="checkbox" class="id_price" value="2"> £250-£300<br>
<input type="checkbox" class="id_price" value="3"> £300-£450<br>

关于javascript - 不打印控制台日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036308/

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