javascript - 如何让多个单选按钮在表单上保持选中状态

标签 javascript jquery html css

下面是我的代码。

<div class="filter_client_search_container">
<div class="filter_search_box" style="margin-top:15px;float:left;margin-right:5px;width:349px;">
    <div class="filter_field">  
        <input type="radio" id="allindividuals" name="clienttype">
        <label for="allindividuals" style="width:260px">All Individuals</label>
    </div>
    <div class="filter_field">
        <input type="radio" id="byindividual" name="clienttype">
        <label for="byindividual" style="width:146px;">By Individual Name</label>
        <input type="text" name="clientcriteria">
    </div>
    <div class="filter_search_criteria" style="left:186px;">
        <input type="button" name="APPLY" value="Apply">
    </div>
</div>

<div class="filter_search_box" style="margin-top:15px;float:left;width:352px;margin-right:5px;">
    <div class="filter_field">  
        <input type="radio" id="allcompanies" name="clienttype" value="allclients" checked>
        <label for="allcompanies" style="width:260px;">All Companies</label>
    </div>
    <div class="filter_field">
        <input type="radio" id="bycompany" name="clienttype" value="byclient">
        <label for="bycompany" style="width:149px;">By Company Name</label>
        <input type="text" name="byclientcriteria">
    </div>
    <div class="filter_search_criteria" style="left:189px;">
        <input type="button" name="APPLY" value="Apply">
    </div>
</div>
</div>

我在这里得到的是“filter_client_search_container”div,其中包含两个过滤器搜索框 div。每个过滤器搜索框都有自己的几个单选按钮,我想要的是我应该能够在第一个过滤器搜索框中选中一个单选按钮,然后在第二个过滤器搜索框中选中另一个。但是我只允许在一个页面中检查一个单选按钮,因此我不能检查多个单选按钮??

但为什么我不能使用这种方法来完成这个...我本以为两个过滤器搜索框会使单选按钮彼此独立...

有什么想法吗?

最佳答案

单选按钮由 name 属性链接。所有的name="clienttype"单选按钮只能选择1个,如果要选择多个,改成checkbox,或者改name的值。

重新阅读您的帖子后,您似乎需要为一个 div 中的单选按钮指定相同的名称,然后为另一个 div 中的单选按钮指定不同的名称。例如,像这样:

<div class="filter_client_search_container">
<div class="filter_search_box" style="margin-top:15px;float:left;margin-right:5px;width:349px;">
    <div class="filter_field">  
        <input type="radio" id="allindividuals" name="clienttype1">
        <label for="allindividuals" style="width:260px">All Individuals</label>
    </div>
    <div class="filter_field">
        <input type="radio" id="byindividual" name="clienttype1">
        <label for="byindividual" style="width:146px;">By Individual Name</label>
        <input type="text" name="clientcriteria">
    </div>
    <div class="filter_search_criteria" style="left:186px;">
        <input type="button" name="APPLY" value="Apply">
    </div>
</div>

<div class="filter_search_box" style="margin-top:15px;float:left;width:352px;margin-right:5px;">
    <div class="filter_field">  
        <input type="radio" id="allcompanies" name="clienttype2" value="allclients" checked>
        <label for="allcompanies" style="width:260px;">All Companies</label>
    </div>
    <div class="filter_field">
        <input type="radio" id="bycompany" name="clienttype2" value="byclient">
        <label for="bycompany" style="width:149px;">By Company Name</label>
        <input type="text" name="byclientcriteria">
    </div>
    <div class="filter_search_criteria" style="left:189px;">
        <input type="button" name="APPLY" value="Apply">
    </div>
</div>
</div>

注意 name 属性的区别。

关于javascript - 如何让多个单选按钮在表单上保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22082979/

相关文章:

javascript - 让辅助方法在状态初始更新时运行

javascript - 从 php json 解码中检索 json 数组

javascript 跟踪多个图像并将其放置在图像之上

html - 当显示页面出现在 ListView 中时,后退按钮被隐藏

html - 我无法将图片和文字正确内联

javascript - 如何在 reactjs 中使用 pug 模板引擎

javascript - 在 Angular 中打印 [object HTMLElement]

javascript - 如果值已经存在,如何将字符添加到值的末尾?

jQuery 触发变量变化

javascript - 根据选择启用/禁用输入框