动态表中的JQuery更改复选框

时间:2017-06-09 13:43:31

标签: php jquery checkbox

我正在创建一个基于使用PHP从MySQL查询返回的行的表:

while($row = mysqli_fetch_array($query_results)){
    echo "<tr>";
    echo "<td>$row[Title]</td>";
    echo "<td>$row[Name]</td>";
    echo "<td><input class='row-$i' id='one-$i' type='checkbox' ($row[check_one]==1 ? 'checked' : '')/></td>";
    echo "<td><input class='row-$i' id='two-$i' type='checkbox' ($row[check_two]==1 ? 'checked' : '')/></td>";
    echo "</tr>";
    $i++
}

我想要发生的是,如果选中第一个复选框并检查第二个复选框,则第一个复选框将取消选中。我原本打算使用单选按钮来完成此操作,但用户还需要能够自由选中或取消选中第一个复选框(即两者都可以一次取消选中),因此单选按钮不起作用。所以我需要做的是为change元素检测two-$i,但我不确定如何为动态元素执行此操作。在正常情况下,JQuery将是:

$('#two').change(function(){...});

那么如何修改它来处理动态ID?

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
class CheckIn extends BackendInterfacePageController
{
    protected $viewPath = '/panels/page/check_in';
    // we need this extra because this controller gets called by another page
    // and that page needs to know how to submit it.
    protected $controllerActionPath = '/ccm/system/panels/page/check_in';

    public function canAccess()
    {
        return $this->permissions->canApprovePageVersions() || $this->permissions->canEditPageContents();
    }

    public function on_start()
    {
        parent::on_start();
        if ($this->page) {
            $v = CollectionVersion::get($this->page, "RECENT");

            $this->set('publishDate', $v->getPublishDate());
            $this->set('publishErrors', $this->checkForPublishing());
        }
    }

    protected function checkForPublishing()
    {
        $c = $this->page;
        // verify this page type has all the items necessary to be approved.
        $e = Loader::helper('validation/error');
        if ($c->isPageDraft()) {
            if (!$c->getPageDraftTargetParentPageID()) {
                $e->add(t('You haven\'t chosen where to publish this page.'));
            }
        }
        $pagetype = $c->getPageTypeObject();
//        if (is_object($pagetype)) {
//            $validator = $pagetype->getPageTypeValidatorObject();
//            $e->add($validator->validatePublishDraftRequest($c));
//        }

        if ($c->isPageDraft() && !$e->has()) {
            $targetParentID = $c->getPageDraftTargetParentPageID();
            if ($targetParentID) {
                $tp = Page::getByID($targetParentID, 'ACTIVE');
                $pp = new Permissions($tp);
                if (!is_object($tp) || $tp->isError()) {
                    $e->add(t('Invalid target page.'));
                } else {
                    if (!$pp->canAddSubCollection($pagetype)) {
                        $e->add(
                            t(
                                'You do not have permissions to add a page of this type in the selected location.'
                            )
                        );
                    }
                }
            }
        }

        return $e;
    }
&#13;
function change(val)
{
  if(val=='one-1')
    document.getElementById('two-2').checked=false
  else
    document.getElementById('one-1').checked=false
}
&#13;
&#13;
&#13;

这对你有所帮助 快乐的编码。

答案 1 :(得分:0)

$(document).ready(function(){
    $("#table > input[type='checkbox']").on("click",function(){
         $('#table > input:checkbox').not(this).removeAttr('checked');
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="table">
  First<input type="checkbox" id="one-1" value="one-1" /><br>
  Second<input type="checkbox" id="two-2" value="two-2" /><br>
  Third<input type="checkbox" id="two-2" value="two-2" />

</div>