0
<select id="ddl_example4" name="ddl_example4">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<option value="6">item6</option>
</select>

</br>
<textarea rows="4" cols="50" id="tarea" onblur="myFunction()"></textarea>

<script>

function myFunction()
{

var x = document.getElementById("tarea");
iteminput = document.getElementById("ddl_example4");
var v = x.value.substring(5);
alert(v);

function setSelectedIndex(s, v) {
for ( var i = 0; i < s.options.length; i++ ) {
if ( s.options[i].text == v ) {
s.options[i].selected = true;
return;
}
}
}
}    
</script>

在模糊时,我可以在警报框中获取值,但无法在下拉框中选择相同的值。我从其他站点复制了函数 setselectedindex。请让我知道我在这方面出了什么问题

还是有更好的方法来使用 javascript 实现相同的输出?

谢谢

4

5 回答 5

1

您将setSelectedIndex函数包含在myFunction函数中,但实际上从未调用过它。

试试这个:

function myFunction()
{
    var x = document.getElementById("tarea");
    iteminput = document.getElementById("ddl_example4");
    var v = x.value.substring(5);
    setSelectedIndex(iteminput, v);
}    

function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        if ( s.options[i].text == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}
​
于 2012-12-22T06:06:41.540 回答
1
<select id="ddl_example4" onblur="selectOption()" name="ddl_example4">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<option value="6">item6</option>
</select>

/*
You can achieve it using jquery like this


Function selectOption()
{
$("#ddl_example4 option:selected").val();
OR
$("#ddl_example4").val();

*/To selct specific option just set it value......i,e to select "Item5"please use

$("#ddl_example4").val(5);
}
于 2012-12-22T06:11:28.683 回答
0
<select id="ddl_example4" name="ddl_example4">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<option value="6">item6</option>
</select>

</br>
<textarea rows="4" cols="50" id="tarea" onblur="myFunction()"></textarea>

<script>

function myFunction()
{

var x = document.getElementById("tarea");
var iteminput = document.getElementById("ddl_example4");
var v = x.value.substring(5);
alert(v);
setSelectedIndex(iteminput, v);
}

function setSelectedIndex(s, v) 
{
    for ( var i = 0; i < s.options.length; i++ ) 
    {
        if ( s.options[i].text == v ) 
        {
            s.options[i].selected = true;
            break;
        }
    }
}


</script>
于 2012-12-22T06:07:38.757 回答
0
<select id="ddl_example4" name="ddl_example4">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<option value="6">item6</option>
</select>

</br>
<textarea rows="4" cols="50" id="tarea" onblur="myFunction()"></textarea>

<script>

function myFunction()
{

var x = document.getElementById("tarea");
iteminput = document.getElementById("ddl_example4");
var v = x.value.substring(5);
alert(v);
s= iteminput;
setSelectedIndex(s, v);

}  

function setSelectedIndex(s, v) {
for ( var i = 0; i < s.options.length; i++ ) {
if ( s.options[i].text == v ) {
s.options[i].selected = true;
return;
}
}
}  
</script>

尝试将文本框输入为“rrrrritem2”,然后您将在下拉列表中选择 item2

于 2012-12-22T06:10:29.550 回答
0

功能未正确关闭。检查以下代码。

<select id="ddl_example4" name="ddl_example4">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<option value="6">item6</option>
</select>

</br>
<textarea rows="4" cols="50" id="tarea" onblur="myFunction()"></textarea>
<script>

    function myFunction()
    {

    var x = document.getElementById("tarea");
    iteminput = document.getElementById("ddl_example4");
    var v = x.value.substring(5);
    alert(v);
    }
    function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
    if ( s.options[i].text == v ) {
    s.options[i].selected = true;
    return;
    }
    }
    }   
    </script>
于 2012-12-22T06:11:21.927 回答