我有此代码,并希望在取消选择图像(切换)时删除选择(文本框)。此外,是否可以隐藏文本框,以便在“提交”时传递它,我只能获得选定的图像?
最好的方法是什么?
<html>
<head>
<style type="text/css">
div img {
cursor: pointer;
border: 1px solid #f00;
}
img {
padding: 5px;
}
img.clicked {
padding: 0;
border: 5px solid blue;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script>
function setFormImage(id) {
if (id != '' && !document.getElementById('input_'+id)) {
var img = document.createElement('input');
img.type = 'text';
img.id = 'input_'+id;
img.name = 'images[]';
img.value = id;
document.imageSubmit.appendChild(img);
}
}
$(document).ready(function(){
$('#jqueryimages img').click(function(){
setFormImage(this.id);
});
});
</script>
</head>
<body>
<pre>
</pre>
<div id="jqueryimages" style="float: left; width: 49%;">
<h1>Image Selection</h1>
5. <img src="http://www.handmadepotery.com/home.gif" id="img-5"/>
<br/>
6. <img src="http://www.handmadepotery.com/home.gif" id="img-6"/>
<br/>
7. <img src="http://www.handmadepotery.com/home.gif" id="img-7"/>
<br/>
8. <img src="http://www.handmadepotery.com/home.gif" id="img-8"/>
</div>
<script>
ims = document.getElementsByTagName("img");
for( i=0 ; i<ims.length ; i++ ){
ims[i].onclick=function() {
if (this.className == "clicked") {
this.className = "";
} else {
this.className = "clicked";
}
};
}
</script>
<h1>Form Submit</h1>
<form name="imageSubmit" method="get">
<input type="submit" value="View Selected"/>
</form>
</body>
</html>