-1

it is late and this is probably a very simple fix, i have seen several posts her on stackoverflow covering this, but i cant seem to get this to work right now.

To the case, i have a select with 7 options. All options have a number from 1 to 7 in their value fiels.

html

<select id="thecase" name="thecase">
    <option value="1">Velg hva saken gjelder:</option>
    <option value="2">Transportskade</option>
    <option value="3">Stengt bestilling</option>
    <option value="4">Finansiering</option>
    <option value="5">Ros & ris</option>
    <option value="6">Bedrift</option>
    <option value="7">Annet</option>
</select>

javaScript

var hiddenOpt = jQuery("#option_other");
var target = jQuery('#thecase');
var caseVal = target.val();
hiddenOpt.hide();
target.change(function(){
    alert (caseVal);
    if (target == 'annet') {
        hiddenOpt.show();
    };
});

When i try this bit of code it returns value 1 no mather what. Here is a fiddle: http://jsfiddle.net/Danbannan/QM2U8/

Could someone explain to me what i am missing? Thanks in advance.

4

7 回答 7

2

put var caseVal = target.val(); inside change function. The first time the select value is 1 and that is saved in caseVal, But you need to get the value when the select value changes, henceyou need to get the target.val() inside the change function. you can also use $(this).val()

http://jsfiddle.net/QM2U8/2/

于 2013-10-03T04:43:34.130 回答
2
var caseVal = target.val();

You're getting the value right away, but never changing it in the .change() function

It should be

target.change(function(){
    caseVal = this.value;
    if (target == 'annet') {
         alert (caseVal);
        hiddenOpt.show();
    };
});
于 2013-10-03T04:43:53.730 回答
1
var caseVal = target.val();

This is defined only at the onload event. Hence it is static. So you have to make it dynamic by initializing in change event.

于 2013-10-03T04:45:59.283 回答
0

Try this out:- http://jsfiddle.net/adiioo7/QM2U8/3/

var hiddenOpt = jQuery("#option_other");
var target = jQuery('#thecase');
hiddenOpt.hide();
target.change(function () {
    var caseVal = target.val();
    alert(caseVal);
    if (target == 'annet') {
        hiddenOpt.show();
    };
});
于 2013-10-03T04:43:57.153 回答
0

Your caseVal is get value before event. So you need to change like this:

target.change(function(){
    caseVal = $(this).val();
    alert (caseVal);
});
于 2013-10-03T04:49:32.813 回答
0

Actually by looking at my code.

The right answer to this is:

target.change(function(){
    caseVal = this.value;
        /*Not the target, the caseVal*/
    if (caseVal == '7') {
         alert (caseVal);
        hiddenOpt.show();
    };
});
于 2013-10-03T04:50:37.140 回答
0

Simple Fix:

var hiddenOpt = jQuery("#option_other");
var target = jQuery('#thecase');

hiddenOpt.hide();
target.change(function(){
    var caseVal = target.val();
    alert (caseVal);
    if (target == 'annet') {
        hiddenOpt.show();
    };
});

Your variable caseVal was residing outside the target.change function, so it was getting, and always getting the inital value. Put it inside to update.. voila

于 2013-10-03T04:51:27.073 回答