Any Javascript experts?

What's Hot
A problem doing my head in. I'm using .Net MVC.
In my view I have the following three fields.

<div class="form-group">
            @Html.LabelFor(model => model.Environment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Environment, @ViewBag.EnvironmentList as IEnumerable<SelectListItem>, new { id = "EnvID" })
                @Html.ValidationMessageFor(model => model.Environment, "", new { @class = "text-danger" })
            </div>
        </div> ///This is a drop down

        <div class="form-group">
            @Html.LabelFor(model => model.Physical, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Physical, new { htmlAttributes = new { id = "PHYSID" } })
                    @Html.ValidationMessageFor(model => model.Physical, "", new { @class = "text-danger" })
                </div>
            </div>
        </div> //// This is a checkbox

        <div class="form-group">
            @Html.LabelFor(model => model.ClusterName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ClusterName, new { htmlAttributes = new { @class = "form-control", id = "CLUSTERID" } } )
                @Html.ValidationMessageFor(model => model.ClusterName, "", new { @class = "text-danger" })
            </div>
        </div> /// This is a text field

So if the checkbox is unchecked I call a controller view to populate. If the checkbox is checked I just want it to display 'NONE'

My Java script code is.

$('#PHYSID').click(function() {
        var Phys_isChecked = $('#PHYSID').is(':checked');
        var Env_selectedVal = $("#EnvID").val();
        var OPP_SelectedVal = '@(ViewBag.OppID)';

        if (Phys_isChecked = "true") {

            $('#CLUSTERID').val("NONE");
        }
       
        else {

            $.ajax({
                type: "POST",
                url: "../OPP_Landscape/GetClusterName",
                data: { OPPID: OPP_SelectedVal, ENV: Env_selectedVal },
                cache: false,
                dataType: "json",
                success: function (Cluster) { $('#CLUSTERID').val(Cluster); }
            });
        };
        
    });
   
    
       
        $('#EnvID').change(function() {
            
           
            var Env_selectedVal = $("#EnvID").val();
            alert(Env_selectedVal);
            var OPP_SelectedVal = '@(ViewBag.OppID)';
            alert(OPP_SelectedVal);
            var Phys_isChecked = $('#PHYSID').is(':checked');
            alert(Phys_isChecked);
            
            if (Phys_isChecked = 'false') {
                $.ajax({
                    type: "POST",
                    url: "../OPP_Landscape/GetClusterName",
                    data: { OPPID: OPP_SelectedVal, ENV: Env_selectedVal },
                    cache: false,
                    dataType: "json",
                    success: function (Cluster) { $('#CLUSTERID').val(Cluster); }
               


                });
            }  else if (Phys_isChecked= 'true') {
                alert("Not Checked");

                $('#CLUSTERID').val("NONE");
            };
           
        });

Now the code seems to work, but if I change the checkbox to checked, it fills in the text field with 'None' as expected but if I then change the dropdown it still fills in the textbox even though the if statement is true?


0reaction image LOL 0reaction image Wow! 0reaction image Wisdom

Comments

  • RedRabbitRedRabbit Frets: 486
    edited September 2016
    Not an expert by any means but it looks to me as though you are using assignments in your if statements. Probably want to change the single = to double (or, with this being JS, triple just to be safe).
    0reaction image LOL 0reaction image Wow! 3reaction image Wisdom
  • If I use double or triple nothing happens?

    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MayneheadMaynehead Frets: 1782
    edited September 2016
    I'm not a javascript expert but I thought checking for boolean equality should be something like this?
    if (Phys_isChecked === true) {
    
    or simply
    
    if (Phys_isChecked) {
    
    since you don't need to worry about type-coercion.
    0reaction image LOL 0reaction image Wow! 2reaction image Wisdom
  • JalapenoJalapeno Frets: 6394
    Similarly not an expert - but a pedant - use the same comment symbols throughout, you have variable number of "/"
    Imagine something sharp and witty here ......

    Feedback
    1reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • Maynehead said:
    I'm not a javascript expert but I thought checking for boolean equality should be something like this?
    if (Phys_isChecked === true) {
    
    or simply
    
    if (Phys_isChecked) {
    
    since you don't need to worry about type-coercion.
    That worked thanks using no = signs.

    Cheers (why can't equals syntax be common across languages)
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • That worked thanks using no = signs.


    Cheers (why can't equals syntax be common across languages)
    The === probably didn't work for you because you had "true" in quotes, which makes it a string not a boolean. Remove the quotes and the === should work as well.
    0reaction image LOL 0reaction image Wow! 2reaction image Wisdom
  • Jalapeno said:
    Similarly not an expert - but a pedant - use the same comment symbols throughout, you have variable number of "/"
    At first I gave you Wisdom for that, but then decided it was more of a LOL. My preference is to not have comments, the code should be clear enough without them ;-)
    2reaction image LOL 0reaction image Wow! 1reaction image Wisdom
  • Jalapeno said:
    Similarly not an expert - but a pedant - use the same comment symbols throughout, you have variable number of "/"
    At first I gave you Wisdom for that, but then decided it was more of a LOL. My preference is to not have comments, the code should be clear enough without them ;-)
    Now that DOES deserve a lol!
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • Maynehead said:
    Jalapeno said:
    Similarly not an expert - but a pedant - use the same comment symbols throughout, you have variable number of "/"
    At first I gave you Wisdom for that, but then decided it was more of a LOL. My preference is to not have comments, the code should be clear enough without them ;-)
    Now that DOES deserve a lol!
    I was being serious though.
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
Sign In or Register to comment.