Return selected images

ADli

New Member
#1
I have an HTML code. There must be 2 questions each of them suggest 4 answers which are images. How can I return in result selected images with javascript?
Mã:
<div class="quest">
        <div class="answer">
            <input type="radio"  value="car1"  class="inputimg"/>
            <label for="car1">
                <img src="spare.png">
            </label>
        </div>
        <div class="answer">
            <input type="radio"  value="car2"  class="inputimg" />
            <label for="car2">             
                <img src="spare.png">
            </label>
        </div>
        <div class="answer">
            <input type="radio"  value="car3"  class="inputimg" />
            <label for="car3">
                <img src="spare.png">
            </label>
        </div>
        <div class="answer">
            <input type="radio"  value="car4"  class="inputimg" />
            <label for="car4">
                <img src="spare.png">
            </label>
        </div>
    </div>
 

Admin

Administrator
Thành viên BQT
#2
Please try the below code ,

When you click on the image it will alert the value of the corresponding radio button

Mã:
/* using jquery*/

function test(dhis){
  var val=dhis.attr('src');
  $('#displayimg').append(' <img src="'+val+'">')
}

/* using javascript*/

function test1(dhis){
  var val=dhis.getAttribute('src');
  var div = document.getElementById('displayimg');
  div.innerHTML +=' <img src="'+val+'">'
}
Mã:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quest">
            <div class="answer">
                <input type="radio"  value="car1"  class="inputimg" name="test" id="car1"/>
                <label for="car1" >
                    <img src="spare.png" onclick="test($(this));test1(this)">
                </label>
            </div>
            <div class="answer">
                <input type="radio"  name="test" value="car2"  class="inputimg" id="car2" />
                <label for="car2" >             
                    <img src="spare.png" onclick="test($(this));test1(this)">
                </label>
            </div>
            <div class="answer">
                <input type="radio" name="test" value="car3"  class="inputimg" id="car3"/>
                <label for="car3">
                    <img src="spare.png" onclick="test($(this));test1(this)">
                </label>
            </div>
            <div class="answer">
                <input type="radio" name="test"  value="car4"  class="inputimg"id="car4" />
                <label for="car4">
                    <img src="spare.png" onclick="test($(this));test1(this)">
                </label>
            </div>
        </div>
        
        <div id="displayimg">
        </div>
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top