简体   繁体   English

在for循环内检查无线电输入(仅包含值的输入)

[英]Checked radio input (only those which contain a value) inside a for loop

this is my code: 这是我的代码:

<?php 

if(!empty($wpjobus_resume_skills)) {

for ($i = 0; $i < (count($wpjobus_resume_skills)); $i++) {

?>

    <div style="text-align: center;">
       <span>20%</span>
       <input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='20%' class='slider_value'>
    </div>


    <div style="text-align: center;">
        <span>40%</span>
        <input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='40%' class='slider_value'>
    </div>

    <div style="text-align: center;">
        <span>60%</span>                                    
        <input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='60%' class='slider_value'>
    </div>

    <div style="text-align: center;">
        <span>80%</span>
        <input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='80%' class='slider_value'>
    </div>

    <div style="text-align: center;">
        <span>100%</span>
        <input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='100%' class='slider_value'>                          
    </div>

ok. 好。 I am storing my data in a post meta var wpjobus_resume_skills . 我将数据存储在post meta var wpjobus_resume_skills I want to only checked the inputs which contains a value. 我只想检查包含值的输入。 for example on 1st iteration of the for loop, the input with 80% value should be checked as i have 80% in my var $wpjobus_resume_skills[$i][1] . 例如在for循环的第一次迭代中,应检查具有80%值的输入,因为我在var $wpjobus_resume_skills[$i][1]具有80%的值。 In the second loop lets say 40% is saved then i want to only checked that 40% input. 在第二个循环中,可以说保存了40%,然后我只想检查一下40%的输入。

screenshot: should be like this 屏幕截图:应该像这样

在此处输入图片说明

this is what i am trying which checked all the values: 这是我正在尝试检查所有值的内容:

<?php
if( !empty($wpjobus_resume_skills[$i][1]) )
{
    <script type="text/javascript">
        jQuery('.checkedClass<?php echo $i; ?>').prop('checked', true);
    </script>
}

any help would be appreciated. 任何帮助,将不胜感激。

my array: print_r($wpjobus_resume_skills); 我的数组: print_r($wpjobus_resume_skills);

Array
(
    [0] => Array
        (
            [0] => C++
            [1] => 40%
        )

    [1] => Array
        (
            [0] => Java
            [1] => 60%
        )

    [2] => Array
        (
            [0] => Rails
            [1] => 80%
        )

    [3] => Array
        (
            [0] => HTML 5
            [1] => 100%
        )

    [4] => Array
        (
            [0] => Android
            [1] => 20%
        )

)

This worked for me: 这为我工作:

adding this to the input tag <?php echo ($wpjobus_resume_skills[$i][1]=='100%')?'checked':'';?> 将其添加到输入标签<?php echo ($wpjobus_resume_skills[$i][1]=='100%')?'checked':'';?>

<input style="width:50%; float: none" type='checkbox' class="checkingtheclass checkedClass<?php echo $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' <?php echo ($wpjobus_resume_skills[$i][1]=='100%')?'checked':'';?> value='100%' class='slider_value'>

Do it with PHP code. 用PHP代码来做。

Inside your loop add this check in each tag. 在循环内部,在每个标签中添加此检查。

   <?php echo ($wpjobus_resume_skills[$i][1]=='YOUR_VALUE')?'checked':'';?>

For example 例如

<?php
for ($i = 0; $i < (count($wpjobus_resume_skills)); $i++) {
?>
<input style="width:50%; float: none" type='checkbox' class="checkedClass<?php echo 
   $i; ?>" id='wpjobus_resume_skills[<?php echo $i; ?>][1]' 
  name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='80%' 
 class='slider_value'      <?php echo ($wpjobus_resume_skills[$i][1]=='80%')?'checked':'';?>

 >
 .

give your checkbox the same class no need to change classes for each input this function in jquery to check your checkbox with value you want 给您的复选框相同的类,而无需为每次输入更改类,而在jquery中使用此功能检查您的复选框是否具有所需的值

 function checkValue(Num){
        $('.checkIT').each(function(){
            if($(this).val() == Num){
               $(this).prop('checked',true);
               }
        });
    }

you can call function with 你可以用

checkValue('80%'); // change number as you want

see DEMO 演示

replace your code with this 用这个替换你的代码

<?php 

if(!empty($wpjobus_resume_skills)) {

for ($i = 0; $i < (count($wpjobus_resume_skills)); $i++) {

?>
<div id="lang<?php echo $i; ?>">
    <div style="text-align: center;">
       <span>20%</span>
       <input style="width:50%; float: none" type='checkbox' class='wpjobus_resume_skills' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='20%'>
    </div>


    <div style="text-align: center;">
        <span>40%</span>
        <input style="width:50%; float: none" type='checkbox' class='wpjobus_resume_skills' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='40%'>
    </div>

    <div style="text-align: center;">
        <span>60%</span>                                    
        <input style="width:50%; float: none" type='checkbox' class='wpjobus_resume_skills' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='60%'>
    </div>

    <div style="text-align: center;">
        <span>80%</span>
        <input style="width:50%; float: none" type='checkbox' class='wpjobus_resume_skills' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='80%' />
    </div>

    <div style="text-align: center;">
        <span>100%</span>
        <input style="width:50%; float: none" type='checkbox' class='wpjobus_resume_skills' name='wpjobus_resume_skills[<?php echo $i; ?>][1]' value='100%' >                          
    </div>
</div>

in js 在js中

$(document).ready(function(){
  checkValue('lang1','20%');
  checkValue('lang2','80%');
  checkValue('lang3','100%');
});
function checkValue(IDname,Num){
    $('#'+IDname +' .wpjobus_resume_skills').each(function(){
        if($(this).val() == Num){
           $(this).prop('checked',true);
           }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM