.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - jQuery trying to get value of one select option based on result of another select option - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery trying to get value of one select option based on result of another select option - Stack Overflow

programmeradmin0浏览0评论

have the below Javascript already, and a couple of Select Groups.

When selecting one of the options in one of the groups, it fills the relavent text box with the text() of that option, and the single ID text box with the val(). This all works fine.

but where I am stuck is that , I need to have it , so that when I select an option in one of the select groups, it looks up the option with the same value in the other select group, and then displays the text of that option in the relevant box.

e.g. If I pick 'Option1', it displays 'Option1' in the first text box, and 'item1' in the second text box (and 1 in the ID text box). If I pick 'item3', it displays 'item3' in the second text box, and 'option3' in the first text box (and 3 in the ID text box).

I got as far as using:

var disp = $('#selItem').find('option[value=1]').text();
alert(disp);

but can't get the dynamic value to go with 'option[value=1]'.

I tried:

var thisValue = $('#txtID').val( $(this).val() ); 
alert(thisValue);

but it responds with [object Object].

If anyone can point me in the right direction it would be appreciated.

<html>
<head>
    <script language="JavaScript" src="../Generic/JAVASCRIPT/jquery.js" type="text/javascript"></script>
    <style>
        .InputField{ width:150px;}
    </style>
    <script>
        $(document).ready( function() 
        {
            $('#selOption').change( function() 
                {
                    $('#txtID').val( $(this).val() );
                    $('#txtOption').val($('#selOption option:selected').html());
                }
            );

            $('#selItem').change( function() 
                {
                    $('#txtID').val( $(this).val() );
                    $('#txtItem').val($('#selItem option:selected').html());
                }
            );
        })  
    </script>
</head>


<body>
    <select id="selOption" class="InputField">
        <option value="" disabled="" selected="" style="display:none;">Select One...</option>
        <option value="1">Option1</option>
        <option value="2">Option2</option>
        <option value="3">Option3</option>                      
    </select>

    <select id="selItem" class="InputField">
        <option value="" disabled="" selected="" style="display:none;">Select One...</option>
        <option value="1">item1</option>
        <option value="2">item2</option>
        <option value="3">item3</option>                        
    </select>

    </br>

    <input type="text" name="txtOption" id="txtOption" placeholder="Which Option is it?" class="InputField"/>
    <input type="text" name="txtItem" id="txtItem" placeholder="Which item is it?" class="InputField"/>

    </br></br>

    <input type="text" name="txtID" id="txtID" placeholder="ID Number" class="InputField"/>
</body>
</html>

have the below Javascript already, and a couple of Select Groups.

When selecting one of the options in one of the groups, it fills the relavent text box with the text() of that option, and the single ID text box with the val(). This all works fine.

but where I am stuck is that , I need to have it , so that when I select an option in one of the select groups, it looks up the option with the same value in the other select group, and then displays the text of that option in the relevant box.

e.g. If I pick 'Option1', it displays 'Option1' in the first text box, and 'item1' in the second text box (and 1 in the ID text box). If I pick 'item3', it displays 'item3' in the second text box, and 'option3' in the first text box (and 3 in the ID text box).

I got as far as using:

var disp = $('#selItem').find('option[value=1]').text();
alert(disp);

but can't get the dynamic value to go with 'option[value=1]'.

I tried:

var thisValue = $('#txtID').val( $(this).val() ); 
alert(thisValue);

but it responds with [object Object].

If anyone can point me in the right direction it would be appreciated.

<html>
<head>
    <script language="JavaScript" src="../Generic/JAVASCRIPT/jquery.js" type="text/javascript"></script>
    <style>
        .InputField{ width:150px;}
    </style>
    <script>
        $(document).ready( function() 
        {
            $('#selOption').change( function() 
                {
                    $('#txtID').val( $(this).val() );
                    $('#txtOption').val($('#selOption option:selected').html());
                }
            );

            $('#selItem').change( function() 
                {
                    $('#txtID').val( $(this).val() );
                    $('#txtItem').val($('#selItem option:selected').html());
                }
            );
        })  
    </script>
</head>


<body>
    <select id="selOption" class="InputField">
        <option value="" disabled="" selected="" style="display:none;">Select One...</option>
        <option value="1">Option1</option>
        <option value="2">Option2</option>
        <option value="3">Option3</option>                      
    </select>

    <select id="selItem" class="InputField">
        <option value="" disabled="" selected="" style="display:none;">Select One...</option>
        <option value="1">item1</option>
        <option value="2">item2</option>
        <option value="3">item3</option>                        
    </select>

    </br>

    <input type="text" name="txtOption" id="txtOption" placeholder="Which Option is it?" class="InputField"/>
    <input type="text" name="txtItem" id="txtItem" placeholder="Which item is it?" class="InputField"/>

    </br></br>

    <input type="text" name="txtID" id="txtID" placeholder="ID Number" class="InputField"/>
</body>
</html>
Share Improve this question edited Sep 2, 2013 at 13:20 Nishanth Lawrence Reginold 1,6214 gold badges29 silver badges43 bronze badges asked Sep 2, 2013 at 13:07 IGGtIGGt 2,77910 gold badges45 silver badges66 bronze badges 5
  • $('#selItem').val($(this).val()); works like a charm in firefox...what browser are you using? – Mr.Manhattan Commented Sep 2, 2013 at 13:25
  • I generally work with Chrome, one of the advantages of not being a 'pro' web developer, I get much more control over which browsers my code runs on. – IGGt Commented Sep 2, 2013 at 13:32
  • i gave you an answer...try it out and give feedback :) – Mr.Manhattan Commented Sep 2, 2013 at 13:33
  • In my answer I also change the selected option, was that desired or just needed to get the text value to insert in the field under? – Sergio Commented Sep 2, 2013 at 13:43
  • It wasn't strictly needed, but only because I hadn't considered that. It's actually quite useful for what I want to do with this going forward. – IGGt Commented Sep 2, 2013 at 13:52
Add a ment  | 

4 Answers 4

Reset to default 3

Try this:

$(document).ready(function () {
    $('#selOption').change(function () {
        $('#txtID').val($(this).val());
        $('#txtOption').val($('#selOption option:selected').text());
        $('#selItem').val($(this).val());
        $('#txtItem').val($('#selItem option:selected').text());
    });

    $('#selItem').change(function () {
        $('#txtID').val($(this).val());
        $('#txtItem').val($('#selItem option:selected').text());
        $('#selOption').val($(this).val());
        $('#txtOption').val($('#selOption option:selected').text());
    });
})

Demo here

It's just:

var thisValue = $(this).val();

When you call .val() with an argument, as in:

var thisValue = $("#txtID").val($(this).val());

it doesn't return the value, it returns the jQuery object, to allow for chaining.

To use this to get the text of the other select, do:

var itemText = $("#selItem option[value=" + thisValue + "]").text();

This is totally normal behavior that it returns Object. This is natural jQuery chaining, so You could use something like $(this).val(something).hide();

Please note that $(selector).val('something') sets up value of selected object and returns that object. If You would like to get value, You have to use val() without argument.

try this:

$('#selOption').change( function(){
    $('#txtID').val( $(this).val() );
    $('#txtOption').val($('#selOption option:selected').html());
    $('#selItem').val($(this).val());
    if(!changed){
        changed = true; //preventing recursion
        $('#selItem').trigger('change'); //fire change, to update text field
    } else {
        changed = false;                        
    }
});

works like a charm in my fiddle: http://jsfiddle/FPvxB/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论