【问题标题】:Testing Radio button checked state in JQuery not working correctly测试 JQuery 中的单选按钮检查状态无法正常工作
【发布时间】:2014-05-10 19:38:45
【问题描述】:

我有一组嵌套的 If/Else 语句,旨在确定机器类型、客户和零件来源,以便将使用站点的人引导到正确的部门订购零件。这是我的代码:

$('#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>"); 
        }
    }

}

包含表单的 HTML 块,带有单选按钮:

<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>

但是,if ($('#radio1').is(':checked')) 似乎无法正常工作,因为选择客户类型 2(这将使#radio1 的状态为假)和机器类型 1 不会返回指示此机器及其所选客户无法获得零件。

我对尝试测试此单选按钮状态感到完全困惑,因为当我实际运行该页面时,我发现似乎没有任何东西可以正常工作。

【问题讨论】:

  • 你是什么意思“因为选择客户类型 1 会使 #radio1 的状态为假”?从它的外观来看,如果它被选中,它应该是真的吗? “Machine Type 1”的收音机在哪里?你能发一个jsfiddle.net 来说明这个问题吗?
  • @AlexV.Kostyukov - 似乎仍然没有触发最外层 if 语句的第二部分。
  • @Tilwin.Joy Gah,这是我的错字。它应该是“......因为选择客户类型 2 会使#radio1 的状态为假”

标签: javascript jquery html if-statement radio-button


【解决方案1】:

试试这个,

$("input:radio[name=radio]").click(function(){
     // your codes
  });

【讨论】:

  • 编辑了我上面的代码;我需要检查单选按钮的状态,作为 JQuery 自动完成字段上的 Select 事件的一部分。很抱歉之前没有说清楚。
猜你喜欢
  • 2012-12-28
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
相关资源
最近更新 更多