<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>将里面的form50事件封装到 函数function confirmRestart(formId) { if (confirm('需重启才能生效,是否立即重启?')) { document.getElementById('form50').submit(); } else { document.getElementById(formId).submit(); } }中
时间: 2023-07-06 07:17:28 浏览: 35
可以这样封装:
```javascript
function confirmRestart(formId) {
if (confirm('需重启才能生效,是否立即重启?')) {
document.getElementById(formId).submit();
}
}
```
在函数中,将传入的 `formId` 参数作为 `document.getElementById()` 方法的参数,获取到对应的表单元素,然后调用 `submit()` 方法提交表单。同时加入判断,如果用户点击了确认按钮,则提交 form50 表单,否则提交传入的表单。
相关问题
在<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事件,将原来的弹窗和确定按钮合并为一个操作。当用户点击设置按钮时,会弹出一个确认框,如果用户点击“确定”按钮,则会直接提交重启请求,否则不进行任何操作。