一个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基本不懂操作 寻求热心人解答 帮忙 谢谢了
...全文
384 13 打赏 收藏 转发到动态 举报
写回复
用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 显示出来
道光2008 2009-12-14
  • 打赏
  • 举报
回复
把你的需求,说简单点
沧海小鱼 2009-12-14
  • 打赏
  • 举报
回复
回warison2008:

依然没作用
道光2008 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");

Re: 《Linux 系统常用内置命令》  ---------------------------------------内容提要:     01. Linux内置命令概述;两栖命令 echo pwd kill 等等    02. 判断内置命令的方式;help -d [command] 能够正常回显    03. 内置命令在 shell 脚本中的基础应用介绍    04. 内置命令 alias 和 unalias 自定义别名和删除别名;临时生效与永久生效    05. 后台前台 出将入相 腾腾转 任务相关命令 bg fg jobs    06. SHELL 循环的处理方式 break continue exit    07. 常见 builtin 内置命令 eval exit export history    08. 内置命令 read 交互式赋变量;type 判断命令类型    09. 内置命令 ulimit 修改系统资源使用限制;unset 清空变量  本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到最好,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述!  《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》

87,915

社区成员

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

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