87,921
社区成员
发帖
与我相关
我的任务
分享
<input class="addinput" type="text" name="P_class" id="P_class" /><br/><br/>
<input class="addinput" type="text" name="LL_class" id="LL_class"/>
<br/><br/>
<span id="tanchu2">选择费用类别/费用名称</span>
<div style="display:none;border-radius: 10px;width:20%;height:465px;position:fixed;left:5%;top:15%; overflow:hidden; background-color:#999;" id="popup2">
<div style="background-color:#999;width:100%;height:40px;line-height:40px; padding:5px 20px 5px 20px;">
<font color="#000"><strong>请先选择费用类别,再选择费用名称</strong></font></div>
<div style="width:94%; height:360px; margin:0 auto;text-align:center;background-color:#FFF;overflow-y:auto;-webkit-overflow-scrolling: touch">
<div>
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">美工材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">镭射纸</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">丙烯颜料</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">彩墨喷绘</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">粉笔</a>
</div>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
<div>
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">钳工材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">焊工眼镜</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">断线钳</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">8#铁丝</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">焊机</a>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
</div>
<div>
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">裱糊材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">色丁布</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">胶水</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">剪刀</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">花边</a>
</div>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
</div>
<a onClick="popup2.style.display='none'" style="text-align:center;"> 关闭 </a>
</div>
</div>
<script>
// 弹出窗口2的js
(function(){
var tanchu2 = document.getElementById('tanchu2'), popup2 = document.getElementById('popup2');
tanchu2.onclick = function(){
if( popup2.style.display =='none' ){
popup2.style.display = 'block';
}else{
popup2.style.display = 'none';
}
return false;
}
})();
// 添加大类别的js
spans=document.getElementsByTagName("a");
for(i=0;i<spans.length;i++)
{if(spans[i].className=="mingcheng")
{ spans[i].onclick=function()
{ document.getElementById('P_class').value=this.innerHTML;
}}}
// 添加费用名称并关闭窗口2的js
spans=document.getElementsByTagName("a");
for(i=0;i<spans.length;i++){
if(spans[i].className=="mcdetail"){
spans[i].onclick=function(){
document.getElementById('LL_class').value=this.innerHTML;
var parent = this.parentNode;
var parentCate = parent.getElementsByClassName('mingcheng')[0].innerHTML;
document.getElementById('P_class').value = parentCate ;
popup2.style.display='none';
}
}
}
// 选中大类别的js
window.onload = function(){
var divs = document.getElementsByTagName("p");
var len = divs.length;
for(var i=0;i<len;i++){
divs[i].onclick = function(){
for(var j=0;j<len;j++){
divs[j].style.backgroundColor = "#e8e8e8";
}
this.style.backgroundColor = "#999";
};
}
};
</script>
拿去吧。上面写的代码里P_class写错成p_class了var parent = this.parent;
改成
var parent = this.parentNode;
var parent = this.parent;
var parentCate = parent.getElementsByClassName('mingcheng')[0].innerHTML;
document.getElementById('p_class').value = parentCate ;
就这段代码没有起作用,还请大侠指点!
<div>
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">美工材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">镭射纸</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">丙烯颜料</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">彩墨喷绘</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">粉笔</a>
</div>
每个大类小类用个div包起来
spans=document.getElementsByTagName("a");
for(i=0;i<spans.length;i++){
if(spans[i].className=="mcdetail"){
spans[i].onclick=function(){
document.getElementById('LL_class').value=this.innerHTML;
var parent = this.parent;
var parentCate = parent.getElementsByClassName('mingcheng')[0].innerHTML;
document.getElementById('p_class').value = parentCate ;
popup2.style.display='none';
}
}
}
文本框1 <input type="text" id="text1" value="" /> <br />
文本框2 <input type="text" id="text2" value="" /> <br />
下面是弹出的div框
<div id="box">
<div>
<h4 class="main">大类1</h4>
<span class="sub">小类1-1</span>
<span class="sub">小类1-2</span>
<span class="sub">小类1-3</span>
<span class="sub">小类1-4</span>
</div>
<div>
<h4 class="main">大类2</h4>
<span class="sub">小类2-1</span>
<span class="sub">小类2-2</span>
<span class="sub">小类2-3</span>
</div>
</div>
<script type="text/javascript">
$(function(){
$("#box").on("click", ".sub", function(event){
$("#text1").val($(this).closest("div").find(".main").text());
$("#text2").val($(this).text());
$("#box").hide();
});
});
</script>
大侠,抱歉,刚刚才看到!
看到后我欣喜若狂,立马开始测试
意外的是点击小类后没有反应
我是js新手,请多多指教!
[/quote]
引入jquery库
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<input class="addinput" type="text" name="P_class" id="P_class" /><br/><br/>
<input class="addinput" type="text" name="LL_class" id="LL_class"/>
<br/><br/>
<span id="tanchu2">选择费用类别/费用名称</span>
<div style="display:none;border-radius: 10px;width:20%;height:465px;position:fixed;left:5%;top:15%; overflow:hidden; background-color:#999;" id="popup2">
<div style="background-color:#999;width:100%;height:40px;line-height:40px; padding:5px 20px 5px 20px;">
<font color="#000"><strong>请先选择费用类别,再选择费用名称</strong></font></div>
<div style="width:94%; height:360px; margin:0 auto;text-align:center;background-color:#FFF;overflow-y:auto;-webkit-overflow-scrolling: touch">
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">美工材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">镭射纸</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">丙烯颜料</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">彩墨喷绘</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">粉笔</a>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">钳工材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">焊工眼镜</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">断线钳</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">8#铁丝</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">焊机</a>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
<p style="width:100px;height:40px; border-radius:5px; line-height:40px; background-color:#e8e8e8;margin:10px;"><a class="mingcheng">裱糊材料</a></p>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">色丁布</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">胶水</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">剪刀</a>
<a class="mcdetail" style="display:inline; line-height:30px; float:left; width:auto; height:30px; padding:3px; margin:0px 3px 0px 3px;">花边</a>
<hr style="height:1px;border:none;border-top:1px dashed #0066CC; width:90%; margin:10px 5% 10px 5%;" />
</div>
<a onClick="popup2.style.display='none'" style="text-align:center;"> 关闭 </a>
</div>
</div>
<script>
// 弹出窗口2的js
(function(){
var tanchu2 = document.getElementById('tanchu2'), popup2 = document.getElementById('popup2');
tanchu2.onclick = function(){
if( popup2.style.display =='none' ){
popup2.style.display = 'block';
}else{
popup2.style.display = 'none';
}
return false;
}
})();
// 添加大类别的js
spans=document.getElementsByTagName("a");
for(i=0;i<spans.length;i++)
{if(spans[i].className=="mingcheng")
{ spans[i].onclick=function()
{ document.getElementById('P_class').value=this.innerHTML;
}}}
// 添加费用名称并关闭窗口2的js
spans=document.getElementsByTagName("a");
for(i=0;i<spans.length;i++)
{if(spans[i].className=="mcdetail")
{ spans[i].onclick=function()
{ document.getElementById('LL_class').value=this.innerHTML;popup2.style.display='none';
}}}
// 选中大类别的js
window.onload = function(){
var divs = document.getElementsByTagName("p");
var len = divs.length;
for(var i=0;i<len;i++){
divs[i].onclick = function(){
for(var j=0;j<len;j++){
divs[j].style.backgroundColor = "#e8e8e8";
}
this.style.backgroundColor = "#999";
};
}
};
</script>
以上的代码的html部分是我写的,js部分是在网上找来套用的,但要实现我的效果对我来说有难度,请大侠指点,不胜感激!
文本框1 <input type="text" id="text1" value="" /> <br />
文本框2 <input type="text" id="text2" value="" /> <br />
下面是弹出的div框
<div id="box">
<div>
<h4 class="main">大类1</h4>
<span class="sub">小类1-1</span>
<span class="sub">小类1-2</span>
<span class="sub">小类1-3</span>
<span class="sub">小类1-4</span>
</div>
<div>
<h4 class="main">大类2</h4>
<span class="sub">小类2-1</span>
<span class="sub">小类2-2</span>
<span class="sub">小类2-3</span>
</div>
</div>
<script type="text/javascript">
$(function(){
$("#box").on("click", ".sub", function(event){
$("#text1").val($(this).closest("div").find(".main").text());
$("#text2").val($(this).text());
$("#box").hide();
});
});
</script>