admin管理员组文章数量:1400167
How can I change the img src of the img using the ajax. I have the location save in my database and I want to set the img src from the value of something data[0]['patient_photo']
I have the html of like this from my image :
<img id="checkup_pic" src="">
Here is my ajax code :
success: function(data) {
$('#valfname').text(data[0]['patient_fname']);
$('#valmname').text(data[0]['patient_mname']);
$('#vallname').text(data[0]['patient_lname']);
$('#valad').text(data[0]['patient_address']);
$('#valcont').text(data[0]['patient_contact_info']);
$('#valsex').text(data[0]['patient_sex']);
$('#valcvilstat').text(data[0]['patient_civil_status']);
$('#valbday').text(data[0]['patient_bday']);
$('#valage').text(data[0]['patient_age']);
$('#valheight').text(data[0]['patient_height']);
$('#valweight').text(data[0]['patient_weight']);
$('#checkup_pic').attr("src","");
How can I change the img src of the img using the ajax. I have the location save in my database and I want to set the img src from the value of something data[0]['patient_photo']
I have the html of like this from my image :
<img id="checkup_pic" src="">
Here is my ajax code :
success: function(data) {
$('#valfname').text(data[0]['patient_fname']);
$('#valmname').text(data[0]['patient_mname']);
$('#vallname').text(data[0]['patient_lname']);
$('#valad').text(data[0]['patient_address']);
$('#valcont').text(data[0]['patient_contact_info']);
$('#valsex').text(data[0]['patient_sex']);
$('#valcvilstat').text(data[0]['patient_civil_status']);
$('#valbday').text(data[0]['patient_bday']);
$('#valage').text(data[0]['patient_age']);
$('#valheight').text(data[0]['patient_height']);
$('#valweight').text(data[0]['patient_weight']);
$('#checkup_pic').attr("src","");
Share
Improve this question
edited Nov 7, 2016 at 9:26
Zakaria Acharki
67.5k15 gold badges78 silver badges106 bronze badges
asked Nov 7, 2016 at 9:20
Jc JohnJc John
1,8592 gold badges41 silver badges78 bronze badges
1
-
1
$('#checkup_pic').attr("src", data[0]['patient_photo']);
...? – Rory McCrossan Commented Nov 7, 2016 at 9:23
3 Answers
Reset to default 6You need to use attr property of jquery and pass your src in the value.
Try below code:
$('#checkup_pic').attr("src",data[0]['patient_photo']);
You could use jQuery functions prop()
or attr()
to set property to the DOM elements :
prop() : Set one or more properties for the set of matched elements.
attr() : Set one or more attributes for the set of matched elements.
So you need just to pass the source of your image data[0]['patient_photo']
to the src
attribute of your img
tag identified by #checkup_pic
, like :
$('#checkup_pic').prop("src", data[0]['patient_photo']);
//Or
$('#checkup_pic').attr("src", data[0]['patient_photo']);
Hope this helps.
Try this:
$.ajax({
url: url, // url of your file from your data is being fetched like index.php
type: 'post',
success: function(data) {
$("#checkup_pic").attr('src', data[0]['patient_photo']);
}
})
本文标签: javascriptChanging my imag src using ajax getting the location from dbStack Overflow
版权声明:本文标题:javascript - Changing my imag src using ajax getting the location from db - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744162797a2593414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论