我有一个表单,在这个表单中我有"local"airport"oneway"roundtrip"按钮。我写了一个javascript代码,这将设置动作属性根据哪个按钮被点击,但问题是,当我错误地点击了错误的按钮,然后点击正确的按钮,动作属性不是切换到正确的按钮路由,但在动作属性我必须刷新页面。我想在不刷新页面的情况下更改action属性值
这是表单
的图像这是我的javascript代码:-var addInputBtn = document.getElementById("addInputBtn");
var myform = document.getElementById("cabform");
var clicked = false;
addInputBtn.addEventListener("click", function(){
if (!clicked) {
myform.setAttribute("action", "/airport");
clicked = true;
}
});
var localBtn = document.getElementById("localBtn");
localBtn.addEventListener("click", function(){
if (!clicked) {
myform.setAttribute("action", "/local");
clicked = true;
}
});
var oneWay = document.getElementById("oneWay");
oneWay.addEventListener("click", function(){
if (!clicked) {
myform.setAttribute("action", "/oneway");
clicked = true;
}
});
var roundTrip = document.getElementById("roundTrip");
roundTrip.addEventListener("click", function(){
if (!clicked) {
myform.setAttribute("action", "/roundtrip");
clicked = true;
}
});
var searchcab = document.getElementById("searchcab");
var paragraphId = document.getElementById("pid");
searchcab.addEventListener("click", function(){
if (!clicked) {
paragraphId.innerHTML = "Please select one of the Button: Airport, local"
searchcab.setAttribute("type", "button");
} else if (clicked) {
searchcab.setAttribute("type", "submit");
}
});
</script>````
Any suggestion, any help would be great thanks🙏🙏🙏
i am trying to setting the action attribute when a user click on a button, problem is i have to do refresh the page to select the another button otherwise it redirect to the wrong route
如果您想要这种行为,那么只需删除if条件。一旦你点击元素,你将clicked
设置为true
,这意味着你的action
属性永远不会更新,直到你刷新,因为clicked
默认是false
。