2 回答
![?](http://img1.sycdn.imooc.com/533e4d470001a00a02000200-100-100.jpg)
TA贡献1842条经验 获得超21个赞
您正在单选按钮上设置属性。而是将其设置在表单上。
最小更改解决方案可能看起来像。
function updatemethod() {
const getBtn = document.getElementById("rdGet");
const postBtn = document.getElementById("rdPost");
if (getBtn.checked) {
document.getElementById("frmid").setAttribute("method", "GET");
}
else if (postBtn.checked) {
document.getElementById("frmid").setAttribute("method", "POST");
}
}
如果您在浏览器开发人员工具中检查该元素,您可以在单击单选按钮时看到方法发生了变化。
还有另一种更简洁的方法是调用函数并传递this.
onchange="updatemethod(this)"
这里this指的是单选按钮元素,现在您不需要进行任何 DOM 树搜索。
现在的功能是
function updatemethod(elem) {
if (elem.checked) {
elem.form.setAttribute("method", elem.value);
}
}
但是,这取决于您value在 html 中是否具有适当的属性。
例如
<input type="radio" value="VALID VALUE HERE" ... />
![?](http://img1.sycdn.imooc.com/533e4c9c0001975102200220-100-100.jpg)
TA贡献1765条经验 获得超5个赞
您可以为此使用 javascript DOM。
function updatemethod() {
let formfrmId = document.getElementById('frmid');
let typeMethod = document.getElementsByName('typeMethod');
let numberRadioTypeMethod = typeMethod.length;
for (i = 0; i < numberRadioTypeMethod; i++ ) {
if (typeMethod[i].checked)
formfrmId.method = typeMethod[i].value;
}
}
使用浏览器控制台,它将为您提供所有 DOM 属性和功能,供您在 javascript 中使用
添加回答
举报