开发者

problem creating function with javascript

I'm having trouble with the following code I have written for a form im creating:

The first two lines specify me hiding a field from the user.

The next thing I try to do is create a function that will set fields to a specific value if the StatusGroup field is set to Distance ed.

After the function is written I want it to be called everytime StatusGroup is changed.

And the last line specifies that I want autoSet() called when the page is first loaded.

Any insight as to why the following is not working?

    $(document).ready(function() {
        $('#deliveryinput').css("position", "absolute");
        $('#deliveryinput').css("left", "-9999999");

        var autoSet = function() {
    if($('#StatusGroup').val() == 'Distance Ed') {
        $('#DeliveryGro开发者_如何转开发up').val( 'mail' );
        $('#NVTGC').val('DIST'); }
    else if($('#StatusGroup').val() != 'Distance Ed'){
            $('#DeliveryGroup').val( 'pickup' );}
}


        $('#StatusGroup').change(function() {
            autoSet();
        });

        autoSet();


    });

I think you guys nailed the javascript, but I whenever I change StatusGroup the corresponding fields dont respond. Am I using the correct form identifiers? Im comparing the SelectId to its value Distance Ed. And same goes for DeliveryGroup and NVTGC.

<label for="StatusGroup">
<span class="field">
span class="<#ERROR name="ERRORStatus">"><b>Status</b></span>
</span>
select id="StatusGroup" name="StatusGroup" size="1" class="f-name" tabindex="4">
<option selected><#PARAM name="StatusGroup"></option>
<option value="Distance Ed">Distance Ed</option>
<option value="Fac/Research">Fac/Research</option>
<option value="Graduate">Graduate</option>
<option value="Undergraduate">Undergraduate</option>
<option value="Staff">Staff</option>
</select><br />
</label>

Nevermind! Got it, thank you all :)


Condition should be if($("#StatusGroup").val() == 'Distance Ed') otherwise you are just comparing two string and not the status group value.

Fixed:

   if($('#StatusGroup').val() == 'Distance Ed') {
       //'DeliveryGroup' = 'mail';
       // assumed you were trying to set the value for a field with id DeliveryGroup
       $('#DeliveryGroup').val('mail');
       $('#NVTGC').val('DIST'); 
    } else if($('#StatusGroup').val() != 'Distance Ed'){
       $('#DeliveryGroup').val('pickup'); 
    }


Your if statements are comparing two strings, which are never going to be equal! I think you probably meant to do this:

if($('#StatusGroup').val() == 'Distance Ed')

Which gets the value of the element with id "StatusGroup". I also assume that you don't want to assign the string "mail" to the string "DeliveryGroup", because that's just not possible. Is "DeliveryGroup" a variable? In that case, get rid of the quotes:

var DeliveryGroup //This line is somewhere else
DeliveryGroup = 'mail'; //Then you can do this in your if statement

As for other issues with your code, you don't need to test the condition again in the else branch - if the first condition fails, it will fall through to the else branch anyway, so why bother testing again?

You could also hide the deliveryinput element with $('#deliveryinput').hide() instead of changing the CSS properties.


next to what my predecessors said, your missing the closing bracket of your autoSet function


These will never be equal, because they're two different string values:

if('#StatusGroup' == 'Distance Ed') {

I think you're looking for the value, so do this:

if($('#StatusGroup').val() == 'Distance Ed') {

...and these won't work at all:

'DeliveryGroup' = 'mail';
'NVTGC' = 'DIST'; 

I assume you want selectors for those as well. Just not sure what they should be.

$('DeliveryGroup').val( 'mail' );  // replace 'DeliveryGroup' with proper selector
$('NVTGC').val('DIST');            // replace 'NVTGC' with proper selector

The two uses of the val()[docs] method are such that with no argument, it gets the value. With an argument, it sets the value.


At a minimum, you're missing the $().val() around your selector: if('#StatusGroup' == 'Distance Ed') { should be if($('#StatusGroup').val() == 'Distance Ed') {


var autoSet = function() {
    if ('#StatusGroup' == 'Distance Ed') {
        'DeliveryGroup' = 'mail';
        'NVTGC' = 'DIST'; 
    }
    else if ('#StatusGroup' != 'Distance Ed'){
        'DeliveryGroup' = 'pickup'; 
    }
}

Both of your conditions will never be true, so nothing happens in this function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜