[英]How to pass radio button value to img link in Javascript?
I am trying to pass the radio button value to Javascript instead of the whole img link but no image is being display.我正在尝试将单选按钮值传递给 Javascript 而不是整个 img 链接,但没有显示图像。 If I pass the whole image link to Javascript the image is able to show in the display.如果我将整个图像链接传递给 Javascript,则图像能够显示在显示器中。
$(document).ready(function () { $(':radio').change(function (e) { //clear the div $('#display').html(''); //update the div $(':radio:checked').each(function (ind, ele) { $('#display').html('<img src="/images/"') + val() + '.png">'); }); }); });
<input type="radio" name="vehicle" value="motor_1"><img src="/images/motor_1.png" width="150px" height="150px">
For jQuery val()
method instead of just calling val()
you want:对于 jQuery val()
方法,而不是仅仅调用val()
你想要的:
$(this).val()
Or the native element property:或本机元素属性:
this.value
@jmp the goal is to display the image when it is checked @jmp 目标是在检查时显示图像
In general, this code would display an img
element when it is clicked通常,此代码在单击时会显示一个img
元素
Code代码
<style>img{ display: none}</style>
<input type="radio" name="vehicle" value="motor_1">
<img src="https://static.independent.co.uk/s3fs-public/thumbnails/image/2018/11/24/16/cat.jpg" >
<script>
(async () => {
let radioBtn = document.querySelector("body > input[name=vehicle]");
radioBtn.onclick = (event) => {
if (radioBtn && radioBtn.checked) {
let img = document.querySelector('img');
img.style.display = 'inherit';
}
}
})();
</script>
In this case, it would seem we want to parameterize the src
string.在这种情况下,我们似乎想要参数化src
字符串。 Does this answer the question?这能回答问题吗? I used the reference from https://stackoverflow.com/a/62853517/112233我使用了来自https://stackoverflow.com/a/62853517/112233的参考
code代码
$('#display').html(`<img src="/images/${ele.value}.png">`);
assuming the ele
is the radio button element.假设ele
是单选按钮元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.