我正在寻找JS / JQuery方法来执行以下操作 1:并排显示一系列油漆芯片和“无”图像 2:单击芯片时 答:用边框突出显示它已被选中 B:将文本字段/可能隐藏字段的值更改为适当的颜色名称 (因此图像应标记为靛蓝,日光浴等,并相应更新)
我在Stackoverflow上发现了这篇文章,但无法使其正常工作:jQuery image picker
这是我目前基于上述链接的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$('div#image_container img').click(function(){
// set the img-source as value of image_from_list
$('input#image_from_list').val( $(this).attr("src") );
});
</script>
</head>
<body>
<div id="image_container">
<img src="images/vermillion.jpg" />
<img src="images/riverway.jpg" />
<img src="images/solaria.jpg" />
</div>
<form action="" method="get">
<input id="image_from_list" name="image_from_list" type="text" value="" />
</form>
</body>
</html>
任何帮助都将非常感谢!!!!
好吧,我最终修复了这个问题,这是我用过的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="image_container">
<img src="images/vermillion.jpg" col="red" border="0" />
<img src="images/riverway.jpg" col="blue" border="0" />
<img src="images/solaria.jpg" col="yellow" border="0" />
</div>
<form action="" method="get">
<input id="image_from_list" name="image_from_list" type="text" value="" />
</form>
<script>
$("div#image_container img").click(function () {
$("div#image_container img").attr("border","0");
$(this).attr("border","4");
$("input#image_from_list").val($(this).attr("col"));
});
</script>
</body>
</html>
答案 0 :(得分:5)
我知道这已经得到了解答但是我开发了一个插件来解决这个问题并得到很好的维护。
答案 1 :(得分:3)
您的代码无效,因为您忘记将jquery语句括在$(document).ready块中。您更新的代码有效,因为您将脚本块移动到底部,但从技术上讲,您应将其封装在document.ready
中这是一个工作版本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#image_container img').click(function(){
//remove border on any images that might be selected
$('#image_container img').removeClass("img_border")
// set the img-source as value of image_from_list
$('#image_from_list').val( $(this).attr("src") );
$('#data_value').val( $(this).attr("id") );
// $('#data_value').val( $(this).data("options").color );
//add border to a clicked image
$(this).addClass("img_border")
});
})
</script>
<style>
.img_border {
border: 2px solid red;
}
</style>
</head>
<body>
<div id="image_container">
<img src="images/vermillion.jpg" id="vermillion"/>
<img src="images/riverway.jpg" id="riverway"/>
<img src="images/solaria.jpg" id="solaria"/>
</div>
<form action="" method="get">
<input id="image_from_list" name="image_from_list" type="text" value="" />
<input id="data_value" type="text" value="" />
</form>
</body>
</html>
答案 2 :(得分:2)
更新了我最后在问题中使用的代码,但也在这里
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="image_container">
<img src="images/vermillion.jpg" col="red" border="0" />
<img src="images/riverway.jpg" col="blue" border="0" />
<img src="images/solaria.jpg" col="yellow" border="0" />
</div>
<form action="" method="get">
<input id="image_from_list" name="image_from_list" type="text" value="" />
</form>
<script>
$("div#image_container img").click(function () {
$("div#image_container img").attr("border","0");
$(this).attr("border","4");
$("input#image_from_list").val($(this).attr("col"));
});
</script>
</body>
</html>