简体   繁体   English

使用选择更改图像和描述

[英]Change Image and description using select

I want to change my image and description based on the selected value in my field.我想根据我的字段中选择的值更改我的图像和描述。 I have done it but the problem is that I can only use the .val() for description and image.我已经做到了,但问题是我只能使用 .val() 进行描述和图像。

I'm in the test stage right now, later after I've done the jQuery I will get the values from the database Here is my Javascript:我现在处于测试阶段,稍后在我完成 jQuery 之后,我将从数据库中获取值这是我的 Javascript:

<script>
var base_url = "<?php echo base_url();?>";
$(document).ready(function() {
    $("#field_6").change(function() {
        $('#description').html($(this).val());
        $('#image').attr("src", base_url + "img/" + $(this).val());
    }).change();
});
</script>

Here is my HTML code:这是我的 HTML 代码:

<select class="form-control" id="field_6" name="field_6">
   <option value="1.jpg">Online Portal for FGC</option>  
   <option value="2.png">Restaurant Management for Wit Cafe</option>  
   <option value="3.jpg">Hotel Reservation for TWIECO</option>  
</select>
<img src="<?php echo base_url()?>img/1.jpg" id="image" class="img-responsive" style="height:300px;width:100%;" name="myImage" /><br>
<label class="form_field">Title Description:  <span id="description"></span>?</label>

The output right now is like this:现在的输出是这样的:
I got the image我得到了图像
and then ex.(1.jpg)然后例如(1.jpg)

My problem is that it will not be good if both image name and description will be the same for example the image name will be The Online portal for FGC.jpg我的问题是,如果图像名称和描述相同,则效果不佳,例如图像名称将是 FGC.jpg 的在线门户
and the description will be like that too.并且描述也会如此。

如果我没记错:

$('#description').html($(this).find( "option:selected" ).text());

Here you go:干得好:

$("#field_6").change(function() {
    $('#description').html($(':selected', this).text());
    $('#image').attr("src", base_url + "img/" + $(this).val());
}).change();
<script>
var base_url = "<?php echo base_url();?>";
$(document).ready(function() {
    $("#field_6").change(function() {

        $('#description').html($(this).find('option:selected').attr("data-description"));
        $('#image').attr("src", base_url + "img/" + $(this).val());

    }).change();
});
</script>

Here, was tired of talking in comments.到这里,已经厌倦了在评论中说话。 your change is on the select box, so you can't get the data attributes of the options without finding them, needed to add that 'find option selected' section.您的更改位于选择框上,因此您无法在未找到选项的情况下获取它们的数据属性,需要添加“已选择的查找选项”部分。

<script>
var base_url = "test.com/";
$(document).ready(function() {
    $("#field_6").change(function() {
        $('#description').html($(this).find('option:selected').attr('data-description'));
        $('#image').attr("src", base_url + "img/" + $(this).find('option:selected').attr('data-img'));
    });
});
</script>

<select class="form-control" id="field_6" name="field_6">
   <option data-description="my description" data-img="test.com/image.jpg" value="1.jpg">Online Portal for FGC</option>  
   <option data-description="my description2" data-img="test.com/image2.jpg" value="2.png">Restaurant Management for Wit Cafe</option>  
   <option data-description="my description3" data-img="test.com/image3.jpg" value="3.jpg">Hotel Reservation for TWIECO</option>  
</select>
<img src="test.com/img/1.jpg" id="image" class="img-responsive" style="height:300px;width:100%;" name="myImage" /><br>
<label class="form_field">Title Description:  <span id="description"></span>?</label>

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

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