输入类型范围输出不适用于动态ID

时间:2017-01-06 07:00:21

标签: javascript php jquery html range

当id值是动态的时,输入类型范围功能不起作用我该怎么做..?



<input name="gdOverallscore" id="gdOverallscore" type="range" min="0" value="0" max="10" step="1" list="ticks" oninput="ageOutputId.value = gdOverallscore.value"/><output id="ageOutputId">0</output>

<h2>This is working fine in my case i have dynamic id which is loop"</h2>
<br>
<input  name="gdskill[<?php echo  $skillLabel['ssid'];?>]"
       id="gdskill[<?php echo  $skillLabel['ssid'];?>]" 
       type="range"
       min="0" value="0" max="10" step="1" 
       list="ticks" 
       oninput="Output.value = gdskill[<?php echo  $skillLabel['ssid'];?>].value"/>
<output id="Output">0</output>

<br>
<h2>
In the second one id values comes like this:
gdskill[1],gdskill[3],gdskill[4],gdskill[6] etc..

</h2>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

尝试使用这样的方式:gdOverallscore[0]之类的ID无效。

 <?php

    $skillLabel['ssid']=10;

    ?>

    <input name="gdOverallscore" id="gdOverallscore" type="range" min="0" value="0" max="10" step="1" list="ticks" oninput="ageOutputId.value = gdOverallscore.value"/><output id="ageOutputId">0</output>


    <input  name="gdskill<?php echo  $skillLabel['ssid'];?>"
           id="gdskill<?php echo  $skillLabel['ssid'];?>" 
           type="range"
           min="0" value="0" max="10" step="1" 
           list="ticks" 
           oninput="Output.value = gdskill<?php echo  $skillLabel['ssid'];?>.value"/>
    <output id="Output">0</output>

多种情况:

<?php

$skillLabel['ssid'][0]=10;
$skillLabel['ssid'][1]=11;

?>

<?php foreach($skillLabel['ssid'] as $key=>$value){?>
<input  name="gdskill<?php echo  $value;?>"
       id="gdskill<?php echo  $value;?>" 
       type="range"
       min="0" value="0" max="10" step="1" 
       list="ticks" 
       oninput="Output<?php echo  $value;?>.value = gdskill<?php echo  $value;?>.value"/>
<output id="Output<?php echo  $value;?>">0</output>

<?php }?>

DEMO

答案 1 :(得分:1)

您还必须更改您的Ouptut Id,否则输出将变得相同,因此它不会工作..

<input name="[]" id="gdskill<?php echo  $skillLabel['ssid'];?>"

 type="range" min="0" value="0" max="10" step="1"

oninput="Output<?php echo  $skillLabel['ssid'];?>.value = gdskill<?php echo  $skillLabel['ssid'];?>.value"/>

<output id="Output<?php echo  $skillLabel['ssid'];?>">0</output>