一个js二级联动 在回显值的问题

沧海小鱼 2009-12-14 03:50:55
现在有一个js二级联动

代码是这样的

<script type='text/javascript'>
function removeOptions(selectObj){
if(typeof selectObj != 'object'){
selectObj = document.getElementById(selectObj);
}
var len = selectObj.options.length;
for(var i=0 ; i<len ;i++){
selectObj.options[0] = null;
}
}
function setSelectOption(selectObj, optionList, firstOption, selected){
if(typeof selectObj != 'object'){
selectObj = document.getElementById(selectObj);
}
removeOptions(selectObj);
var start = 0;
if(firstOption){
selectObj.options[0] = new Option(firstOption,'');
start ++;
}
var len = optionList.length;
for(var i=0; i<len; i++){
selectObj.options[start] = new Option(optionList[i].txt,optionList[i].val);
if(selected == optionList[i].val){
selectObj.options[start].selected = true;
}
start++;
}
}
</script>
<script type='text/javascript'>
----------
数据动态调取 省略
------------
function setCzys(CzSel){
setSelectOption('Czys',cityArr[CzSel],'-请选择-');
}
</script>



我现在想要的效果是 我点击第一个下拉单的时候 希望能把值在回显到另外一个input框(Spr) 以作为表单里的一项数据提交

<select id='CzSel' name='CzSel' onchange='if(this.value!="") setCzys(this.options[this.selectedIndex].value);document.getElementById("Spr").value=this.options[this.selectedIndex].Sprice;'>
<option value='' Sprice='22'>-请选择-</option>
<option value='' Sprice='66'>77</option>
</select>
颜色:<select id='Czys' name='Czys'>
<option value=''>-请选择-</option>
</select>
<input name="Spr" type="text" id="Spr" size="10"/>

这一句是我自己加的
document.getElementById("Spr").value=this.options[this.selectedIndex].Sprice;

对js基本不懂操作 寻求热心人解答 帮忙 谢谢了
...全文
395 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
沧海小鱼 2009-12-15
  • 打赏
  • 举报
回复
非常感谢 搞定

先弄完活的 一会回来
阿非 2009-12-15
  • 打赏
  • 举报
回复

var len = optionList.length;
for(var i=0; i<len; i++){
if(optionList[i]){
selectObj.options[start] = new Option(optionList[i].txt,optionList[i].val);
if(selected == optionList[i].val){
selectObj.options[start].selected = true;
}
start++;
}
}
沧海小鱼 2009-12-15
  • 打赏
  • 举报
回复
<script type='text/javascript'>
function removeOptions(selectObj){
if(typeof selectObj != 'object'){
selectObj = document.getElementById(selectObj);
}
var len = selectObj.options.length;
for(var i=0 ; i<len ;i++){
selectObj.options[0] = null;
}
}
function setSelectOption(selectObj, optionList, firstOption, selected){
if(typeof selectObj != 'object'){
selectObj = document.getElementById(selectObj);
}
removeOptions(selectObj);
var start = 0;
if(firstOption){
selectObj.options[0] = new Option(firstOption,'');
start ++;
}
var len = optionList.length;
for(var i=0; i<len; i++){
selectObj.options[start] = new Option(optionList[i].txt,optionList[i].val);
if(selected == optionList[i].val){
selectObj.options[start].selected = true;
}
start++;
}
}
</script>
<script type='text/javascript'>
var cityArr = [];

cityArr['pu皮质'] = [

{txt:'白色242',val:'3'},

{txt:'白色3',val:'4'},

];

cityArr['giuu'] = [

{txt:'白色',val:'5'},

{txt:'黑色',val:'6'},

];

function setCzys(CzSel){
setSelectOption('Czys',cityArr[CzSel],'-请选择-');
}

</script>


<select id='CzSel' name='CzSel' onchange='javascript:if(this.value!="") setCzys(this.options[this.selectedIndex].value);document.getElementById("Spr").value=this.options[this.selectedIndex].Sprice;'>
<option value='0' Sprice='22'>-请选择-</option>

<option value='pu皮质' Sprice='pu皮质'>pu皮质</option>

<option value='giuu' Sprice='giuu'>giuu</option>

</select>
颜色:<select id='Czys' name='Czys'>
<option value=''>-请选择-</option>
</select>
<input name="Spr" type="text" id="Spr" size="10"/>


这个就是测试代码了
jol_boy 2009-12-15
  • 打赏
  • 举报
回复
这两个动作不冲突的,select的onchange事件中,先去控制联动,然后将当前的select的值赋给一个input显示。

你老说不能显示,肯定是你代码有些地方有问题,得逐条去分析!

把你整个代码贴出来!
阿非 2009-12-15
  • 打赏
  • 举报
回复
cityArr

这个在哪定义的
沧海小鱼 2009-12-15
  • 打赏
  • 举报
回复
一个select的联动

我点击选项的时候
有2个动作
首先是联动
其次是这个select还有一个值要传给input 显示出来
烟雨鹏城 2009-12-14
  • 打赏
  • 举报
回复
把你的需求,说简单点
沧海小鱼 2009-12-14
  • 打赏
  • 举报
回复
回warison2008:

依然没作用
烟雨鹏城 2009-12-14
  • 打赏
  • 举报
回复
改为:
onchange='javascript:if(this.value!="") setCzys(this.options[this.selectedIndex].value);document.getElementById("Spr").value=this.options[this.selectedIndex].Sprice;'>
沧海小鱼 2009-12-14
  • 打赏
  • 举报
回复
这个是可以传值的
我就是找的这个
document.getElementById("Spr").value=this.options[this.selectedIndex].Sprice;
但现在是 我点选这个操作的时候 有2个动作要触发

首先它是联动 之后还要把这个Sprice的值传过去

我就是不知道把这个
document.getElementById("Spr").value=t.options[t.selectedIndex].getAttribute("Sprice");
加在哪里
jol_boy 2009-12-14
  • 打赏
  • 举报
回复
如下代码是没问题的,你传不过去值,可能是你其他地方的代码影响了!检查一下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<select id="s1" onchange="setSlValue(this)">
<option value='' Sprice='22'>-请选择- </option>
<option value='' Sprice='66'>77 </option>
</select>
<input name="Spr" type="text" id="Spr" size="10"/>
</BODY>
</HTML>

<SCRIPT LANGUAGE="JavaScript">
<!--
function setSlValue(t)
{
document.getElementById("Spr").value=t.options[t.selectedIndex].getAttribute("Sprice");
}


//-->
</SCRIPT>

沧海小鱼 2009-12-14
  • 打赏
  • 举报
回复
<option value='' Sprice='22'>-请选择- </option>
<option value='' Sprice='66'>77 </option>

value里是有值的 只是在这里粘贴我没写上

我直接用你这个替换掉原来的 还是一样的啊

如何把这个值传过去的ne
jol_boy 2009-12-14
  • 打赏
  • 举报
回复
你的select的option中value没有给值,自定义了一个Sprice属性,你得去获取这个属性。如下
document.getElementById("Spr").value=this.options[this.selectedIndex].getAttribute("Sprice");

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧