开发者

javascript focus problem

I have a form with 3 fields. Country, state and users

javascript focus problem

I am trying to do the following. when United states is selected as a country, the state field will开发者_如何学编程 show. The problem is that when i use the tab key on the keyboard, it is skipping the state field and its going on the users field. So i tried using the focus property so when i select United states, the state will show + selected, but I had no luck.. Below please find the code I am using

$(document).ready(function () {

 $("#cmbCountries").change(function () {
    $("#cmbCountries option:selected").each(function () {
        if ($(this).text() == "United States") {               

            $("#cmbstate").show(); 
            $("#cmbstate").focus(); 

        }

        else {
            $("#cmbstate").hide();
        }
    });
}).change();   });

Any help please?


Chek this out

And javascript

$(function (){

    $("#cmbstate").hide();

    $("#cmbCountries").change(function () {
        if($(this).val() == 'US')
        {
            $("#cmbstate").show();
            $('#cmbstate').focus()
        }
        else{
            $("#cmbstate").hide();
            $('#users').focus();
        }
    })


})

And html

<input type="text" name="text" id="first" />
<select id="cmbCountries">
    <option value="OTHER">OTHER</option>
    <option value="US">US</option>
    <option value="OTHER1">OTHER1</option>
</select>
 <select id="cmbstate">
     <option value="value">Value 1</option>         
     <option value="value">Value 2</option>
     <option value="value">Value 3</option>
</select>
<select id="users">
    <option value="asf">asdfasdf</option>
    <option value="fadfas">sdffd</option>    
</select>


The Tabindex values must be wrong, try and manually set the Tabindex of each input to 0, 1, 2 respectively and then you should be able to tab between them easily


Please try using this code,

$(document).ready(function () {

 $("#cmbCountries").change(function () {
    $("#cmbCountries option:selected").each(function () {
        if ($(this).text() == "United States") {               

            $("#cmbstate").show(); 
            $("#cmbstate").select();
        }

        else {
            $("#cmbstate").hide();
        }
    });
}).change();   });

changed focus() to select()

I had once done this in my project also which worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜