将变量连接到单选按钮选择器

时间:2015-05-11 18:50:57

标签: javascript jquery html radio-button concatenation

我试图找到已检查单选按钮的值。我正在连接变量' flagName'进入输入选择器,它继续返回undefined。这是我的JQuery:

function filterProjects (searchInput) {

var filter = $(searchInput).val(), count = 0;
var $projects = $(".ProjectDisplay");
$projects.show();
$projects.each(function () {
  var $currentProject = $(this);
  var projectFlags = $(this).data();

  for (var flagName in projectFlags) {
    var flagValue = projectFlags[flagName];
    var checkedInputValue = $("input[name='" + flagName + "']:checked", "#flagSearchForm").val();

    if ((checkedInputValue == "yes" && flagValue == "0") || (checkedInputValue == "no" && flagValue == "1")) {
      $currentProject.hide();
    }
  }

  if ($(this).find(".projectName").text().search(new RegExp(filter, "i")) < 0) {
    $(this).hide();
  }
}); 

}

我试图找到checkedInputValue的值。 flagName和flagValue都返回它们应该的内容。在HTML中,我有一个循环PHP数组的表单来创建多个字段集,每个字段集有三个单选按钮。这是我的HTML的一部分,表单是:

<form id="flagSearchForm" name="flagForm">
<div class="grid-unit grid-unit-9-14">
  <h4>Search by Flag Value</h4>
  <div class="flagSearch">
    <div class="wrapper grids">
    <?php 
    $flag_names = array("hasBib", "hasChoice", "hasEbooks", "hasFrbr", "hasLcd","hasLtp", "hasNlm", "hasPeers", "includeDewey", "includeGovtDocs", "includeNonBooks", "includeOpacUrl", "includeProtection", "includeScores"); ?>

    <?php foreach ($flag_names as $i=>$flag_name):
    ?>
    <div class="flagfields">
      <div class="grid-unit grid-unit-3-14">
        <fieldset>
          <span class="flagName"><?php echo $flag_name; ?></span>
          <input type="button" value="N/A" class="tri-state ignore" id="<?php echo $flag_name; ?>"/>
          <input type="radio" name="<?php echo $flag_name; ?>" value="ignore" id="<?php echo $flag_name; ?>-ignore" class="radio-button" checked><label for="<?php echo $flag_name; ?>-ignore">Ignore</label>
          <input type="radio" name="<?php echo $flag_name; ?>" value="yes" id="<?php echo $flag_name; ?>-yes" class="radio-button"><label for="<?php echo $flag_name; ?>-yes">Yes</label>
          <input type="radio" name="<?php echo $flag_name; ?>" value="no" id="<?php echo $flag_name; ?>-no" class="radio-button"><label for="<?php echo $flag_name; ?>-no">No</label>
        </fieldset>
      </div>
    </div>
    <?php 
    endforeach;
    ?>
    </div>
  </div>
<input type="submit" value="submit" class="flagSearchSubmit">
</div>

我的checkInputValue语法错了吗?

1 个答案:

答案 0 :(得分:0)

我不确定您的过滤器/正则表达式是什么,因此需要完成。这会记录所有选定的输入。

这是一个小提琴:https://jsfiddle.net/c4qbywrt/7/

function filterProjects(searchInput) {

    $('input:radio').each(function () {
        if ($(this).is(':checked')) {

            var $currentProject = $(this);
            var checkedInputValue = $(this).val();
            var flagValue = '0'; // Not sure what is setting this
            if ((checkedInputValue == "yes" && flagValue == "0") || (checkedInputValue == "no" && flagValue == "1")) {
                $currentProject.hide();
            }
            var filter = '';
            if  ($(this).find(".projectName").text().search(new RegExp(filter, "i")) < 0) {
                $(this).hide();
            }
        }
    });
}

$("#searchProjects").keyup(function () {
    filterProjects($(this));
});
$("#flagSearchForm").submit(function (event) {
    event.preventDefault();
    filterProjects($("#searchProjects"));
});