简体   繁体   English

如何将单选按钮值传递给 Javascript 中的 img 链接?

[英]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.

相关问题 如何使用 img(即 href 链接)和单选按钮(值或 id)制作一个函数并转到下一页 - how to make a function out of img (which is href link ) and radio button (value or id) and goes to the next page 如何通过单选按钮将所选值传递给javascript函数 - How to pass selected value to javascript function from radio button 如何使用单选按钮使用HTML5和Javascript更改img src - How to change img src with HTML5 and Javascript using radio button 如何使用JavaScript使用单选按钮和开关盒更改img? - How to change img with JavaScript using radio button and switch case? 如何以反应形式传递单选按钮的值? - How to pass value for radio button in reactive form? 如何使用 jquery 和 PHP 传递单选按钮值? - how to pass radio button value with jquery & PHP? 如何在反应中将单选按钮的值传递给组件 - How to pass value of radio button to component in react 使用 javascript 将单选按钮中的值添加到现有链接 url - Add value from radio button to existing link url use javascript 使用单选按钮将值从html形式传递给javascript函数 - pass value from html form to javascript function with the use of radio button 想要将单选按钮 html 中选择的值传递给 controller javascript - want to pass value selected in radio button html to controller javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM