简体   繁体   English

单击选定的单选按钮选项时如何更改图像

[英]how to change the image when click on selected radio button option

In my php file default image is there.在我的 php 文件中,默认图像在那里。 i add some radio button options.我添加了一些单选按钮选项。 here is my code这是我的代码

 <form method="post" action="" enctype="multipart/form-data"> <input type="radio" value="<img src='images/image1.jpg'>"><img src="images/image1.jpg" width="50px" height="50px"><br/> <input type="radio" value="<img src='images/image2.jpg'>"><img src="images/image2.jpg" width="50px" height="50px"><br/> <input type="radio" value="Both" />Both<br/> </form> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $("input:radio[type=radio]").click(function() { var value = $(this).val(); $('#showoption').html(value); }); </script> <label>Value</label> /// default image <img src="images/image2.jpg" width="50px" height="50px"/> <div id="showoption"> </div> </label>

When i click on radio button selected image to be showing.当我单击单选按钮选择要显示的图像时。 I want at the same time default image to be hide.我希望同时隐藏默认图像。 How to solve this issue.如何解决这个问题。

As you just want to hide the default image try this code, changes done in jquery :由于您只想隐藏默认图像,请尝试使用此代码,在 jquery 中完成的更改:

<form method="post" action="" enctype="multipart/form-data">
<input type="radio" value="<img src='images/image1.jpg'>"><img src="images/image1.jpg" width="50px" height="50px"><br/>
<input type="radio" value="<img src='images/image2.jpg'>"><img src="images/image2.jpg" width="50px" height="50px"><br/>
<input type="radio" value="Both" />Both<br/>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$("input:radio[type=radio]").click(function() {
var value = $(this).val();
$('#showoption').html(value);

$('.def-img').css({'display':'none'}); // this will disappear the image

});
</script>

<label>Value</label>
    // default image 

 <div class="def-img"><img src="images/image2.jpg" width="50px" height="50px"/></div>
 <div id="showoption"></div>
</label>

I think you want like this: changes on in Jquery and add id to your default image.我认为您想要这样:在 Jquery 中更改并将 id 添加到您的默认图像。

 <style> .thumbnail img{ width:50px; height:50px;} </style> <form method="post" action="" enctype="multipart/form-data"> <input type="radio" name="r1" value="<img src='images/image1.jpg'>"><img src="images/image1.jpg" width="50px" height="50px"><br/> <input type="radio" name="r1" value="<img src='images/image2.jpg'>"><img src="images/image2.jpg" width="50px" height="50px"><br/> <input type="radio" name="r1" value="Both" />Both<br/> </form> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $("input:radio[type=radio]").click(function() { var value = $(this).val(); $('#showoption').html(value); $('.defaultImage').hide(); }); </script> <label>Value</label> /// default image <div class="defaultImage"> <img src="images/image2.jpg" width="50px" height="50px"/> </div> <div id="showoption" class="thumbnail" style="width:50px;height:50px"> </div> </label>

Just change the src value on click radio button.只需在单击单选按钮上更改 src 值。 and must set radio button name attribute.并且必须设置单选按钮名称属性。

<form method="post" action="" enctype="multipart/form-data">
<input type="radio" value="image1.jpg" name="show"><img src="images/image1.jpg" width="50px" height="50px"><br/>
<input type="radio" value="image2.jpg" name="show"><img src="images/image2.jpg" width="50px" height="50px"><br/>
<input type="radio" value="Both" name="show"/>Both<br/>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$("input:radio[type=radio]").click(function() {
var value = $(this).val();
$('#image').attr('src', 'images/'+value);
    });
</script>
<label>Value</label>
<img src="images/image2.jpg" width="50px" height="50px" id="image" />
</label>

Here you are, try this code let me if it's fine for you.给你,试试这个代码,如果它适合你,让我试试。

 $("input:radio[type=radio]").click(function() { var value = $(this).val(); if(value == 1) { $('#image2').hide(); $('#image1').show(); } else if(value == 2) { $('#image2').show(); $('#image1').hide(); } else if(value == 'Both') { $('#image2').show(); $('#image1').show(); } $('#showoption').html(value); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <form method="post" action="" enctype="multipart/form-data"> <input type="radio" name="image" value="1"> <input type="radio" name="image" value="2"> <input type="radio" value="Both" name="image" />Both<br/> </form> <label>Value</label> /// default image <img src="images/image2.jpg" width="50px" height="50px" id="image2" style="display:none;" alt="image2"/> <img src="images/image1.jpg" width="50px" height="50px" id="image1" style="display:none;" alt="image1"> <div id="showoption"> </div> </label>

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

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