0

I have a set of nested If/Else statements that are intended to determine the type of machine, customer, and part source in order to direct the person using the site to the correct department for ordering parts. Here is the code I have:

$('#partName').catcomplete({
            minLength: 2,
            source: data,
            select: function ( event, ui ) {
                $("#partNumber").text(ui.item.Number);
                $("#partDesc").text(ui.item.PartDesc);
                // Determine machine type
                if (ui.item.Type == "All Machines") {
                    $("#machineType").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Message for parts that go to all machines</p></div></div>");
                }
                else if (ui.item.Type == "Machine Type 1") {
                    $("#machineType").html("<div class='ui-widget'><div class = 'ui-state-error ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> Message for parts ONLY for Machine Type 1</p></div></div>");
                }
                else if (ui.item.Type == "Machine Type 2") {
                    $("#machineType").html("<div class='ui-widget'><div class = 'ui-state-error ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> Message for parts ONLY for Machine Type 2 </p></div></div>");
                }
               // How determine who to transfer call to
               if ($('#radio1').is(':checked')){
               if (ui.item.type == "Machine Type 1" || "All Machines") {
                   if (ui.item.Source == 1) {
                      $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #1</p></div></div>");
                   }
                   if (ui.item.Source == 2) {
                      $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #2</p></div></div>");
                   }
               }
              if (ui.item.type == "Machine Type 2") {
                 if (ui.item.Source == 1) {
                    $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #1</p></div></div>");
                 }
                 if (ui.item.Source == 2) {
                        $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #2</p></div></div>"); 
                     }
               }
}
else {
    if (ui.item.type == "Machine Type 1") {
        if (ui.item.Source == 1) {
            $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-error ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> This machine and its parts are not available to this customer. Please try your search again.</p></div></div>");
        }
        if (ui.item.Source == 2) {
            $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-error ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> This machine and its parts are not available to this customer. Please try your search again.</p></div></div>");
        }
    }
    if (ui.item.type == "Machine Type 2" || "All Machines") {
        if (ui.item.Source == 1) {
            $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #3</p></div></div>");
        }
        if (ui.item.Source == 2) {
            $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: 6px;'></span> Text containing extension #4</p></div></div>"); 
        }
    }

}

The block of HTML containing the form, with radio buttons:

<div id="radio">
            <strong>Select Machine: </strong>
            <input type="radio" value="fsa" id="radio1" name="radio" checked><label for="radio1">Customer Type 1</label>
            <input type="radio" value="evo" id="radio2" name="radio"><label for="radio2">Customer Type 2</label>
        </div>
        <label for="partName"><strong>Part Name:</strong></label>
        <input id="partName" size="50">
        <input class="ui-state-default ui-corner-all" type="button" id="clearPartName" onClick="clear_all()" value="Clear Search">
        </p>
      <div id="partData">
        <div id="partNumberField">
            <p><strong>PBS Part Number:</strong></p>
            <div style="margin: 2px" class="ui-widget-content" id="partNumber">&nbsp;</div>
        </div>
        <div id="partDescField">
            <p><strong>Part Description:</strong></p>
            <div style="margin: 2px"  class="ui-widget-content" id="partDesc">&nbsp;</div>
        </div>
        <div id="machineTypeField">
            <div style="margin: 2px" id="machineType">&nbsp;</div>
        </div>
        <div id="partObtainField">
            <p><strong>Source for Part:</strong></p>
            <div style="margin: 2px" id="partSource">&nbsp;</div>
        </div>
      </div>

However, if ($('#radio1').is(':checked')) doesn't appear to be working properly, as selecting Customer Type 2 (which would make the state of #radio1 false) and Machine Type 1 does not return the error message indicating that this machine and its parts are not available to the selected customer.

I'm utterly baffled by trying to test this Radio button state, as nothing I've found seems to work properly when I actually run the page.

  • 1
    http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery – AuthorProxy Mar 31 '14 at 12:55
  • What do you mean " as selecting Customer Type 1 would make the state of #radio1 false"? From the look of it it should make it true if it is selected? and where is the radio for "Machine Type 1"? can you make a http://jsfiddle.net to illustrate the problem? – T J Mar 31 '14 at 12:56
  • @AlexV.Kostyukov - that still doesn't appear to trigger the second part of the outermost if statement. – Karie Adair Mar 31 '14 at 13:04
  • @Tilwin.Joy Gah, that's a typo on my part. It should be "...as selecting Customer Type 2 would make the state of #radio1 false" – Karie Adair Mar 31 '14 at 13:09

1 Answers1

3

Try this,

$("input:radio[name=radio]").click(function(){
     // your codes
  });
Vinu Sebastian
  • 568
  • 3
  • 16
  • Edited my code above; I'm needing the check of the radio button's state to go off as part of the Select event on a JQuery autocomplete field. Sorry for not making that clear previously. – Karie Adair Mar 31 '14 at 13:23