将<form target="sys" id="form50" method="post" action="/cgi-bin/reboot"> <input type="submit" name="reboot" id="reboot" value="重启" style="border:none;background:none;cursor:pointer;outline:none;"/> </form>和 函数function confirmRestart(formId) { if (confirm('需重启才能生效,是否立即重启?')) { document.getElementById('form50').submit(); } else { document.getElementById(formId).submit(); } }封装到js文件中
时间: 2023-07-06 17:17:29 浏览: 44
你可以将这段代码封装到一个独立的 JavaScript 文件中,比如名为 `reboot.js`,内容如下:
```javascript
function confirmRestart(formId) {
if (confirm('需重启才能生效,是否立即重启?')) {
document.getElementById('form50').submit();
} else {
document.getElementById(formId).submit();
}
}
```
然后,在 HTML 文件中引入这个 JavaScript 文件:
```html
<script src="reboot.js"></script>
```
这样就可以在 HTML 文件中调用 `confirmRestart` 函数了。
相关问题
在<td ><form target="ip" id="form28" name="form4" method="post" action="/cgi-bin/vgaic.cgi"> <input type="text" name="ip" id="ip" size="16" /> <label for="name1_submit"></label> <input type="submit" name="name1_submit" id="name1_submit" onclick="if(confirm('需重启才能生效,是否立即重启?点确定立即重启')) {} else{return false;}" value="设置" /> <label for="label50"></label> <input type="reset" name="name_abord16" id="label50" value="清除" /> </form></td>的弹窗中增加一个重启按钮 <td ><form target="sys" id="form20" method="post" action="/cgi-bin/reboot"> <input type="submit" name="reboot" id="reboot" value="重启" /> </form>
在这段代码中,可以在`<td>`标签内添加一个新的`<form>`标签,用于提交重启请求。具体代码如下:
```
<td>
<form target="ip" id="form28" name="form4" method="post" action="/cgi-bin/vgaic.cgi">
<input type="text" name="ip" id="ip" size="16" />
<label for="name1_submit"></label>
<input type="submit" name="name1_submit" id="name1_submit" onclick="if(confirm('需重启才能生效,是否立即重启?点确定立即重启')) {} else {return false;}" value="设置" />
<label for="label50"></label>
<input type="reset" name="name_abord16" id="label50" value="清除" />
</form>
<form target="sys" id="form29" method="post" action="/cgi-bin/reboot">
<input type="submit" name="reboot" id="reboot" value="重启" />
</form>
</td>
```
这段代码中,我们新增了一个`<form>`标签,`target`属性设置为`sys`,用于提交重启请求。按钮的`name`属性设置为`reboot`,当用户点击该按钮时,会向服务器发送一个重启请求。
在<td ><form target="ip" id="form28" name="form4" method="post" action="/cgi-bin/vgaic.cgi"> <input type="text" name="ip" id="ip" size="16" /> <label for="name1_submit"></label> <input type="submit" name="name1_submit" id="name1_submit" onclick="if(confirm('需重启才能生效,是否立即重启?点确定立即重启')) {} else{return false;}" value="设置" /> <label for="label50"></label> <input type="reset" name="name_abord16" id="label50" value="清除" /> </form></td>的onclick弹窗中增加一个已有的确定重启的按钮 <td ><form target="sys" id="form20" method="post" action="/cgi-bin/reboot"> <input type="submit" name="reboot" id="reboot" value="重启" /> </form>
如果您想在弹窗中添加一个已有的确定重启按钮,可以修改原有的onclick事件,将原有的确定按钮和弹窗合并。具体代码如下:
```
<td>
<form target="ip" id="form28" name="form4" method="post" action="/cgi-bin/vgaic.cgi">
<input type="text" name="ip" id="ip" size="16" />
<label for="name1_submit"></label>
<input type="submit" name="name1_submit" id="name1_submit" onclick="if(confirm('需重启才能生效,是否立即重启?')) {document.getElementById('form29').submit();} else {return false;}" value="设置" />
<label for="label50"></label>
<input type="reset" name="name_abord16" id="label50" value="清除" />
</form>
<form target="sys" id="form29" method="post" action="/cgi-bin/reboot">
<input type="submit" name="reboot" id="reboot" value="重启" />
</form>
</td>
```
在这段代码中,我们修改了原来的onclick事件,将原来的弹窗和确定按钮合并为一个操作。当用户点击设置按钮时,会弹出一个确认框,如果用户点击“确定”按钮,则会直接提交重启请求,否则不进行任何操作。