<div class="row cl"> <label class="form-label col-xs-4 col-sm-2">图片</label> <div class="formControls col-xs-8 col-sm-9"> <script type="text/javascript" src="js/selimage.js"></script> <input class="image" type="text" name="image" id="image" onclick="selimage('image');" placeholder="请选择图片" readonly="readonly" /> </div> </div> <div class="row cl"> <label class="form-label col-xs-4 col-sm-2">详情页图</label> <div class="formControls col-xs-8 col-sm-9"> <script type="text/javascript" src="js/selimage.js"></script> <input class="image" type="text" name="image1" id="image1" onclick="selimage('image1');" placeholder="请选择图片" readonly="readonly" /> </div> </div>两个input标签都同时使用selimage脚本,selimage脚本代码如下:function selimage() { window .open( "upload/preUpload.action", "", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=400,height=240,top=" + (screen.availHeight - 240) / 2 + ",left=" + (screen.availWidth - 400) / 2 + ""); } function savefile() { window .open( "upload/preFiles.action", "", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=400,height=240,top=" + (screen.availHeight - 240) / 2 + ",left=" + (screen.availWidth - 400) / 2 + ""); } 在上传图片时,第二张图会覆盖第一张图的文本框,该如何更改代码
时间: 2023-06-14 07:08:00 浏览: 144
你可以在selimage函数中传入一个参数,用来指定具体是哪个input标签被点击触发了该函数,然后分别给两个input标签指定不同的id和name,以避免覆盖。修改后的代码如下:
```
function selimage(inputName) {
window.open(
"upload/preUpload.action",
"",
"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=400,height=240,top="
+ (screen.availHeight - 240) / 2
+ ",left="
+ (screen.availWidth - 400) / 2 + ""
);
// 根据传入的参数设置对应的input标签
document.getElementById(inputName).setAttribute('name', inputName);
document.getElementById(inputName).setAttribute('id', inputName);
}
```
同时,你需要在HTML中将两个input标签的onclick事件分别指定为`selimage('image')`和`selimage('image1')`,并分别指定不同的id和name,如下所示:
```
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">图片</label>
<div class="formControls col-xs-8 col-sm-9">
<script type="text/javascript" src="js/selimage.js"></script>
<input class="image" type="text" name="image" id="image" onclick="selimage('image');" placeholder="请选择图片" readonly="readonly" />
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">详情页图</label>
<div class="formControls col-xs-8 col-sm-9">
<script type="text/javascript" src="js/selimage.js"></script>
<input class="image" type="text" name="image1" id="image1" onclick="selimage('image1');" placeholder="请选择图片" readonly="readonly" />
</div>
</div>
```
阅读全文