用js往select里面赋值的问题,很简单

shenshiluguo 2010-01-07 05:09:28
现在我有一个需求,现在我有一个select框 我要用js给这个select赋值 要显示的值在这个select里面已经有了 但是问题是

我现在赋值text的话等于给select框加了一个选项,但是我现在要复制的时候只取到了select框里面的name,没有Value
请问怎么做?
...全文
11211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝色lord 2011-09-29
  • 打赏
  • 举报
回复
orteus= true 是什么意思呀?
asharpnet 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ahywg 的回复:]
<script>
function test(val){
  var sel=document.getElementById('sel');
  for(var i=0;i <sel.options.length;i++)
  {
  if(sel.options[i].text==val)
  {
  sel.options[i].selected=true;
  break;
  }
  }
}
</script>
<select id="sel" name="name" orteus="true">
    <option> </option>
    <option value="1">AAA </option>
</select>
<input type="text" stype="width:100px;" onblur="test(this.value)" />
[/Quote]
jinjiangting 2010-01-07
  • 打赏
  • 举报
回复
同上
一弗楚 2010-01-07
  • 打赏
  • 举报
回复

<script>
function test(val){
var sel=document.getElementById('sel');
for(var i=0;i<sel.options.length;i++)
{
if(sel.options[i].text==val)
{
sel.options[i].selected=true;
break;
}
}
}
</script>
<select id="sel" name="name" orteus="true">
<option> </option>
<option value="1">AAA </option>
</select>
<input type="text" stype="width:100px;" onblur="test(this.value)" />
xmliy 2010-01-07
  • 打赏
  • 举报
回复
<input id="text1" type="text" />
<select id="name" name="name" orteus="true">
<option></option>
<option value="1">AAA</option>
</select>



window.onload = function() {
var input = document.getElementById('text1');
input.onkeyup = function() {
var select = document.getElementById('name');
for (var i = 0, count = select.options.length; i < count; i++) {
if (select.options[i].text == input.value) {
select.selectedIndex = i;
break;
}
}
}
}
shenshiluguo 2010-01-07
  • 打赏
  • 举报
回复
我感觉我也没说明白

这么说吧

我现在有一个select和一个text
现在我的select是这样的
<select name="name" orteus="true">
<option></option>
<option value="1">AAA</option>
</select>
我现在想在text里面输入AAA的时候就让select显示AAA,注意text里面不能输入value值
怎么做?
sundotLei 2010-01-07
  • 打赏
  • 举报
回复
不明白啥意思?

87,907

社区成员

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

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