我有一个表格fontVortMetodo
,我将其称为
var fM = document.forms["fontVortMetodo"];
用户可以选择将表格提交给三个PHP页面之一。我让他首先通过带有select
名称的无线电按钮做出选择。然后,他应该按下一个按钮,该按钮将表单元素的结果解散到选定的页面。
对于此射击,我使用了该功能
function destinu() {
if (fM.elements("select")[0].checked) {
fM.action = "private_php/Zamenhofa.php";
} else if (fM.elements("select")[1].checked) {
fM.action = "private_php/intereuropeco.php";
} else if (fM.elements("select")[2].checked) {
tutm = true;
fM.action = "private_php/tutmondeco.php";
}
}
有一个错误:
TypeError
:fM.elements("select")[0].checked
不是函数。
也许我应该尝试
var destiny = getElementsByName("select")
然后使用if (destiny[0].checked)
或if (destiny[0].checked == true)
。
我不知道有人建议我使用它,而且对于JavaScript,我没有参考文本。我在哪里可以找到jQuery的好教程,尽管我更喜欢使用JavaScript Pure来做所有操作?
使用纯JS
- 使用
dataset
属性存储动作。 - 使用此选择器
[name="select"]:checked
获取检查收音机按钮。
document.querySelector('button').addEventListener('click', function(e) {
e.preventDefault();
var checked = document.querySelector('[name="select"]:checked');
var action = checked.dataset.action;
var form = document.querySelector('#fontVortMetodo');
form.setAttribute('action', action);
var tutm = checked.dataset.tutm !== undefined;
console.log("Action: " + action);
console.log('Is tutm: '+ tutm);
//$form.submit(); // This is if you need to to submit the form.
});
<form id='fontVortMetodo' name='fontVortMetodo'>
<input type='radio' name='select' data-action="private_php/Zamenhofa.php">
<input type='radio' name='select' data-action="private_php/intereuropeco.php">
<input type='radio' name='select' data-action="private_php/tutmondeco.php" data-tutm='true'>
<button>Submit</button>
</form>