doudui2229 2017-08-12 20:53
浏览 21
已采纳

为复选框添加需要的旧值

I have multiple checkboxes that go through validation.. so i need the checkboxes that were picked to remain there..

this are some of them

                   <div class="col-sm-3 col-xs-6 padding-4-tb">
                        <input type="checkbox" name="skills[]" value="PHP"> PHP
                    </div>
                    <div class="col-sm-3 col-xs-6 padding-4-tb">
                        <input type="checkbox" name="skills[]" value="Python"> Python
                    </div>
                    <div class="col-sm-3 col-xs-6 padding-4-tb">
                        <input type="checkbox" name="skills[]" value="Ruby"> Ruby
                    </div>
                    <div class="col-sm-3 col-xs-6 padding-4-tb">
                        <input type="checkbox" name="skills[]" value="Rails"> Rails
                    </div>

as you can see the value doesn't contain numbers.. so how can i do it ??

  • 写回答

1条回答 默认 最新

  • dthl8036 2017-08-12 21:02
    关注

    Your question is not completely clear, but I assume you are submitting a form and then rending the form with validation errors, and you don't want to lose the user's selections. If you're just using old-school PHP and HTML in the same file, you would do something like this:

    <div class="col-sm-3 col-xs-6 padding-4-tb">
        <input type="checkbox" name="skills[]" value="PHP"<?php if(in_array('PHP', $skills))?> checked<?php } ?>> PHP
    </div>
    <div class="col-sm-3 col-xs-6 padding-4-tb">
        <input type="checkbox" name="skills[]" value="Python"<?php if(in_array('Python', $skills))?> checked<?php } ?>> Python
    </div>
    <div class="col-sm-3 col-xs-6 padding-4-tb">
        <input type="checkbox" name="skills[]" value="Ruby"<?php if(in_array('Ruby', $skills))?> checked<?php } ?>> Ruby
    </div>
    <div class="col-sm-3 col-xs-6 padding-4-tb">
        <input type="checkbox" name="skills[]" value="Rails"<?php if(in_array('Rails', $skills))?> checked<?php } ?>> Rails
    </div>
    

    The net is that you need to add the "checked" attribute to each previously selected checkbox. You determine whether or not each box was selected by checking for the presence of it's value in the array of skills values sent to the server on submit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改