单选按钮验证

时间:2014-03-10 12:04:54

标签: javascript jquery html

我正在创建一个表单,其中单选按钮单击事件检查其他表单元素的值。我希望无线电将被检查或取消选中,并取消其他人的价值。 我的HTML

<input id="pDisplayPrice1" name="pDisplayPrice" type="radio" value="yes" onclick="displayPriceFormat();">
<span class="help-inline">Yes</span>
<input id="pDisplayPrice2" name="pDisplayPrice"  type="radio" value="no" >
<span class="help-inline">No</span>

和disPriceFormat()函数是

function displayPriceFormat() {
    var areaPing = 0;

    var areaPerUnitPrice = $("#pPerUnitPrice").val();
    if (areaPerUnitPrice != "") {
        var areaPerUnitPriceUnit = $("#pPerUnitPriceUnit").val();
        var areaShow = areaPerUnitPrice + '/' + areaPerUnitPriceUnit
        areaPing = 1;
    }

    if (areaPing != 0) {
        // radio button will be checked
    } else {
        //radio button will unchecked

    }

我试过了

<input onclick="displayPriceFormat(e);"/>
<script>
 ......
  e.preventDefault(); /// return false;
 .......
</script>

1 个答案:

答案 0 :(得分:0)

我没有看到任何html输入,其id是:pPerUnitPrice 你在这一行中使用过它:

  var areaPerUnitPrice = $("#pPerUnitPrice").val();

但我认为以下信息可以帮助您:

您可以勾选复选框,或者不选中,如下所示:

    if($("#id_of_checkbox").attr("checked")){ 
    //checked
    }
   else {
    //unchecked

   }

您可以选中以下复选框:

$("#id_of_checkbox").prop('checked',true);

你可以选中这样的取消选中框:

$("#id_of_checkbox").prop('checked',false);