【发布时间】:2018-07-06 04:15:56
【问题描述】:
我正在努力实现这一目标:
- 有 3 个单选按钮和 3 个 div,每个按钮都包含一个 img 标签。
- 点击任何单选按钮后,其他 2 个 div 将被隐藏。
- 在选择任何单选按钮时,因为 1 个 div 是可见的。我只需要从该 div 中获取 img src 并将其应用于 id 为“testing”的输入元素的值(属性)。
这是我的代码:
var radOne = $('#graphic-one-name'),
radTwo = $('#graphic-two-name'),
radThree = $('#graphic-three-name'),
radParent = $('#testing');
radOne.on('change', function() {
$('.two, .three').hide();
$('.one').show();
radParent.attr("value", ".one.attr('src')");
});
radTwo.on('change', function() {
$('.one, .three').hide();
$('.two').show();
});
radThree.on('change', function() {
$('.two, .one').hide();
$('.three').show();
});
.one,
.two,
.three {
width: 100px;
height: 100px;
border: 1px solid black;
background-image: url('http://via.placeholder.com/100x100');
}
.two {
background: #023432
}
.three {
background: red
}
.one {
background: #fff
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="graphics-select">
<div>
<input class="graph-init" type="radio" name="graphic-one-name" id="graphic-one-name" value="' . $mono_gram_font_graphic_1_sku . '" />
<label for="graphic-one-name"><img class="image-one-graphics" alt="" height="auto" width="auto" />One</label>
</div>
<div>
<input class="graph-init" type="radio" name="graphic-one-name" id="graphic-two-name" value="' . $mono_gram_font_graphic_2_sku .'" />
<label for="graphic-two-name"><img class="image-two-graphics" alt="" height="auto" width="auto" />Two</label>
</div>
<div>
<input class="graph-init" type="radio" name="graphic-one-name" id="graphic-three-name" value="' . $mono_gram_font_graphic_3_sku .'" />
<label for="graphic-three-name"><img class="image-three-graphics" alt="" height="auto" width="auto" />Three</label>
</div>
<div class="parent">
<div class="one">
<img src="http://via.placeholder.com/100x100">
</div>
<div class="two">
<img src="http://via.placeholder.com/100x100">
</div>
<div class="three">
<img src="http://via.placeholder.com/100x100">
</div>
</div>
<input type="text" id="testing" value="test" class="imagerep">
【问题讨论】:
-
只是为了确保我正确理解您的问题:您想要图像源值,例如
abc.jpg,具体取决于收音机的选择? -
是的,一旦单击单选按钮。取决于可见的 div(其中包含 img)。我需要捕获 img src 并将其分配给最后一个输入元素的 value 属性(其 id 为“testing”)
标签: javascript jquery html