检查所选的单选按钮

时间:2015-12-16 09:42:01

标签: javascript jquery html css

我正在使用this question来查看我检查过的单选按钮以及我没有做什么,但我无法弄清楚我做错了什么。



Dim tempforecast = New With {Key .forecast = New Object}
Dim sFile As String = SimpleTools.RWFile.ReadFile("c:\\testjson\\test.json")
Dim root = JsonConvert.DeserializeAnonymousType(sFile, tempforecast)
Dim tempsimpleforecast = New With {Key .simpleforecast = New Object}
Dim forecast = jsonConvert.DeserializeAnonymousType(root.forecast.ToString(), tempsimpleforecast)
Dim templstforecastday = New With {Key .forecastday = New Object}
Dim simpleforecast = JsonConvert.DeserializeAnonymousType(forecast.simpleforecast.ToString(), templstforecastday)
Dim lstforecastday = simpleforecast.forecastday

For Each jforecastday In lstforecastday

    Dim tempDate = New With {Key .date = New Object, .high = New Object, .low = New Object}
    Dim forecastday = JsonConvert.DeserializeAnonymousType(jforecastday.ToString(), tempDate)

    Dim tempDateDetail = New With {Key .day = "", .month = "", .year = ""}
    Dim fcDateDetail = JsonConvert.DeserializeAnonymousType(forecastday.date.ToString(), tempDateDetail)


    Weather_Forcast.ForcastDate = fcDateDetail.day.ToString() + "/" + fcDateDetail.month.ToString() + "/" + fcDateDetail.year.ToString()


    Dim temphighDetail = New With {Key .celsius = "", .fahrenheit = ""}
    Dim highDetail = JsonConvert.DeserializeAnonymousType(forecastday.high.ToString(), temphighDetail)

    Dim templowDetail = New With {Key .celsius = "", .fahrenheit = ""}
    Dim lowDetail = JsonConvert.DeserializeAnonymousType(forecastday.low.ToString(), templowDetail)

    Weather_Forcast.highCelsius = Decimal.Parse(highDetail.celsius.ToString())
    Weather_Forcast.lowCelsius = Decimal.Parse(lowDetail.celsius.ToString())

    Weather_Forcast.highFahrenheit = Decimal.Parse(lowDetail.fahrenheit.ToString())
    Weather_Forcast.lowFahrenheit = Decimal.Parse(lowDetail.fahrenheit.ToString())


    Weather_Forcast09_Result.Add(Weather_Forcast)

Next

$(document).ready(function() {

  function isEmpty() {
    $('#main_form').find('input[type="radio"]').each(function() {
      var name = $(this).attr("name");
      if ($('input:radio[name="' + name + '"]:checked').val() === 'undefined') {
        $('input:radio[name="' + name + '"]:checked').parents('.form_field').find('.form_label').addClass('not_valid');
      }
    });
  }

  $("a").click(function(e) {
    e.preventDefault();
    isEmpty();
  })
});

.form_label.not_valid {
  color: red;
}




当我执行<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="main_form"> <table> <tbody> <tr class="form_field radio_buttons"> <th class="form_label">Your product</th> <td colspan="5"> <input type="radio" name="your_product" id="your_product_1" value="Goods"> <label class="label-style" for="your_product_1">Goods</label> <input type="radio" name="your_product" id="your_product_2" value="In-between/both"> <label class="label-style" for="your_product_2">In-between/both</label> <input type="radio" name="your_product" id="your_product_3" value="Services"> <label class="label-style" for="your_product_3">Services</label> </td> </tr> <tr class="form_field radio_buttons"> <th class="form_label">Clients</th> <td colspan="5"> <input type="radio" name="clients" id="clients_1" value="Businesses"> <label class="label-style" for="clients_1">Businesses</label> <input type="radio" name="clients" id="clients_2" value="In-between/both"> <label class="label-style" for="clients_2">In-between/both</label> <input type="radio" name="clients" id="clients_3" value="Customers"> <label class="label-style" for="clients_3">Customers</label> </td> </tr> </tbody> </table> </form> <a href="#">click</a>我退出时,如果我已经检查了按钮组的值(重复3次),并且未定义(3次),如果我没有选择任何我得到6次未定义的东西。因此,如果我的逻辑是正确的,我的标签应该获得类console.log($('input:radio[name="' + name + '"]:checked').val())并变为红色。但我必须做错事,我的猜测是if条件。我无法弄清楚我在这里失踪了什么。

如果未选中单选按钮组,我想将标签设为红色。

3 个答案:

答案 0 :(得分:1)

评论:

  1. $('input:radio[name="' + name + '"]:checked').val() === 'undefined'应更改为$('input:radio[name="' + name + '"]').is(':checked')或其他内容。目前,只有当您选中的收音机的值为'undefined'时才会输入。

  2. 如果您想检查名称没有检查值,并更改其父级的类,则应使用$('input:radio[name="' + name + '"]'),而不使用:checked,这会使结果为空jquery对象。

  3. 您也可以通过toggleClassdemo jsfiddle完成所有操作,如果您选中无效的群组,则可以通过再次点击该检查按钮来删除无效的群组。

    // Get name
    var name = $(this).attr("name");
    // Get ref so we don't need to retrieve it twice.
    var $targets = $('input:radio[name="' + name + '"]');
    //Find their parents, and toggle the class by check if the elements are checked or not.
    $targets.parents('.form_field').find('.form_label').toggleClass('not_valid', !$targets.is(':checked'));
    

    改变原始代码:

    $(document).ready(function() {
    
      function isEmpty() {
        $('#main_form').find('input[type="radio"]').each(function() {
          var name = $(this).attr("name");
          
          // Check if the there's any radio with given name is checked.
          if (!$('input:radio[name="' + name + '"]').is(':checked')) {
            $('input:radio[name="' + name + '"]').parents('.form_field').find('.form_label').addClass('not_valid');
          }
        });
      }
    
      $("a").click(function(e) {
        e.preventDefault();
        isEmpty();
      })
    });
    .form_label.not_valid {
      color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="main_form">
      <table>
        <tbody>
          <tr class="form_field radio_buttons">
            <th class="form_label">Your product</th>
            <td colspan="5">
              <input type="radio" name="your_product" id="your_product_1" value="Goods">
              <label class="label-style" for="your_product_1">Goods</label>
              <input type="radio" name="your_product" id="your_product_2" value="In-between/both">
              <label class="label-style" for="your_product_2">In-between/both</label>
              <input type="radio" name="your_product" id="your_product_3" value="Services">
              <label class="label-style" for="your_product_3">Services</label>
            </td>
          </tr>
          <tr class="form_field radio_buttons">
            <th class="form_label">Clients</th>
            <td colspan="5">
              <input type="radio" name="clients" id="clients_1" value="Businesses">
              <label class="label-style" for="clients_1">Businesses</label>
              <input type="radio" name="clients" id="clients_2" value="In-between/both">
              <label class="label-style" for="clients_2">In-between/both</label>
              <input type="radio" name="clients" id="clients_3" value="Customers">
              <label class="label-style" for="clients_3">Customers</label>
            </td>
          </tr>
    
        </tbody>
      </table>
    </form>
    <a href="#">click</a>

答案 1 :(得分:0)

$(document).ready(function() {

      function isEmpty() {
        $('#main_form').find('input[type="radio"]').each(function() {
          var name = $(this).attr("name");

          if ($('input:radio[name="' + name + '"]').is(":not(:checked)")) {

            $('input:radio[name="' + name + '"]').parents('.form_field').find('.form_label').addClass('not_valid');
          }
        });
      }
  $("a").click(function(e) {
  e.preventDefault();
  isEmpty();
  })
});

答案 2 :(得分:0)

试试这个:

&#13;
&#13;
$(document).ready(function() {

  function isEmpty() {
    $('#main_form').find('input[type="radio"]').each(function() {
      var name = $(this).attr("name");
      console.log(name, $('input:radio[name="' + name + '"]').is(":checked"))
      if (!$('input:radio[name="' + name + '"]').is(":checked")) {
        console.log(name, "Error");
        $('input:radio[name="' + name + '"]').parents('.form_field').find('.form_label').addClass('not_valid');
      }
      else{
        $('input:radio[name="' + name + '"]').parents('.form_field').find('.form_label').removeClass('not_valid');
      }
    });
  }

  $("a").click(function(e) {
    e.preventDefault();
    isEmpty();
  })
});
&#13;
.not_valid {
  color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="main_form">
  <table>
    <tbody>
      <tr class="form_field radio_buttons">
        <th class="form_label">Your product</th>
        <td colspan="5">
          <input type="radio" name="your_product" id="your_product_1" value="Goods">
          <label class="label-style" for="your_product_1">Goods</label>
          <input type="radio" name="your_product" id="your_product_2" value="In-between/both">
          <label class="label-style" for="your_product_2">In-between/both</label>
          <input type="radio" name="your_product" id="your_product_3" value="Services">
          <label class="label-style" for="your_product_3">Services</label>
        </td>
      </tr>
      <tr class="form_field radio_buttons">
        <th class="form_label">Clients</th>
        <td colspan="5">
          <input type="radio" name="clients" id="clients_1" value="Businesses">
          <label class="label-style" for="clients_1">Businesses</label>
          <input type="radio" name="clients" id="clients_2" value="In-between/both">
          <label class="label-style" for="clients_2">In-between/both</label>
          <input type="radio" name="clients" id="clients_3" value="Customers">
          <label class="label-style" for="clients_3">Customers</label>
        </td>
      </tr>

    </tbody>
  </table>
</form>
<a href="#">click</a>
&#13;
&#13;
&#13;