2017-06-19 70 views
0

其实我需要替换图像的网址,当我点击图像,然后打开一个弹出窗口与输入字段,我把网址,并保存它,但使用此方法。点击任何图像打开弹出并更改图像url像

喜欢这个:http://jsfiddle.net/eDMmy/9/

//Set up the dialog box 
 
$("#myDialog").dialog({ 
 
    autoOpen : false, 
 
    modal  : true, 
 
    title  : "A Dialog Box", 
 
    buttons : { 
 
       'OK' : function() { 
 
        var textValue = $('#myTextBox').val(); 
 
        var image1 = document.getElementById("image1"); 
 
        image1.src=textValue; 
 
        
 
       }, 
 
       'Close' : function() { 
 
        alert('The Close button was clicked'); 
 
        $(this).dialog('close'); 
 
       } 
 
       } 
 
});
.img { 
 
    height:200px; 
 
    width:200px; 
 
}
<img class='img' src="http://media1.santabanta.com/full5/Nature/Animals/animals-89a.jpg" onclick="ChangeUrl()" id="image1"> 
 
    
 
<div id="myDialog"> 
 
    change url 
 
    <input type="text" id="myTextBox" /> 
 
</div> 
 
    <script> 
 
     function ChangeUrl(){ 
 
    $("#myDialog").dialog("open"); 
 

 
    var image1 = document.getElementById("image1"); 
 
      $("#myTextBox").val(image1.src); 
 
    image1.src= url; 
 
     }</script>

+0

是什么问题? – tech2017

+0

我需要多个图像,点击图像,并改变这个图像不是另一个图像。 EX:我添加3图像,当我点击2没有图像,只改变2没有图像相同,当我点击1没有图像,只改变1没有图像,当点击3没有图像,只改变3没有图像就是它。 –

回答

0

要设置的,而不是从中读取文本字段,这样做,而不是:

var image1 = document.getElementById("image1"); 
var url = $("#myTextBox").val(); 
image1.src= url; 
+0

感谢您的答案“Nayish”,但 \t 我需要多个图像单击图像,并更改此图像不是另一个图像。 EX:我添加3图像,当我点击2没有图像,只改变2没有图像相同,当我点击1没有图像,只改变1没有图像,当点击3没有图像,只改变3没有图像就是它。 –

0

你只需要的是目标你点击的图像。 .data()在这种情况下能够传递参数很重要。检查此解决方案。

http://jsfiddle.net/eDMmy/687/

//Set up the dialog box 
 
$("#myDialog").dialog({ 
 
    autoOpen: false, 
 
    modal: true, 
 
    title: "A Dialog Box", 
 
    buttons: { 
 
    'OK': function() { 
 
     var img_to_update = $(this).data('link'); 
 
     var textValue = $('#myTextBox').val(); 
 
     document.getElementById(img_to_update).src = textValue; 
 
    }, 
 
    'Close': function() { 
 
     //alert('The Close button was clicked'); 
 
     $(this).dialog('close'); 
 
    } 
 
    } 
 
});
.img { 
 
    height: 200px; 
 
    width: 200px; 
 
}
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
 
    
 
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script> 
 

 
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" /> 
 
<img class='img' src="http://media1.santabanta.com/full5/Nature/Animals/animals-89a.jpg" onclick="ChangeUrl(this.id)" id="image1"> 
 
<img class='img' src="http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg" onclick="ChangeUrl(this.id)" id="image2"> 
 
<img class='img' src="https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png" onclick="ChangeUrl(this.id)" id="image3"> 
 

 
<div id="myDialog"> 
 
    change url 
 
    <input type="text" id="myTextBox" /> 
 
</div> 
 
<script> 
 
    function ChangeUrl(x) { 
 
    //$("#myDialog").dialog("open"); 
 
    $("#myDialog") 
 
     .data('link', x) 
 
     .dialog('open'); 
 
    var url = document.getElementById(x).src; 
 
    $('#myTextBox').val(url); 
 
    } 
 
</script>

+0

感谢您的回答“tech2017”其实我在我的HTML页面生成器中使用此代码,所以我需要避免图像ID和onclick =“ChangeUrl(this.id)”上的图像标记或删除图像ID和onclick =“ChangeUrl this.id)“后更改图像网址为干净的代码。你可以帮我吗。 –

+0

这个http://jsfiddle.net/eDMmy/688/ – tech2017

+0

哇!真棒!非常感谢你为我们提供了完美的答案“tech2017”其实我需要这个。谢谢 –