如何设置为打开基于
这里是代码我写如何设置为打开基于
<form>
<input type="radio" name="cand" value="fr" onclick="alert('You Have Selected Fresher Level \nPlease Click Next To Proceed');"> Fresher<br><br>
<input type="radio" name="cand" value="ex" onclick="alert('You Have Selected Experienced Level \nPlease Click Next To Proceed');"> Experienced<br><br>
<input type="button" onclick="location.href='http://google.com';" value="Next" />
</form>
的2个独立的单选按钮上的提交按钮的特定链接如果假设用户选择选项1(比如:新生)然后链接1(新鲜形式的链接)应该是开放的。 或如果选择选项2即经历过,则有经验的链接应该打开。
请解释提供答案,我是新来的HTML和JavaScript。
在下面的示例中,我为每个radio
添加一个属性。当用户点击按钮时,我们获取该属性(data-href
),并使用window.location.href
在那里传输用户。如果你觉得更好,你可以使用value
。请注意,我删除了内联点击功能并为其instad使用了一个事件。
$('#clickButton').click(function(){
var url = $('input[name=cand]:checked').data('href');
//Goto url
window.location.href = url;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="radio" name="cand" value="fr" data-href="https://www.stackoverflow.com" onclick="alert('You Have Selected Fresher Level \nPlease Click Next To Proceed');" > Fresher<br><br>
<input type="radio" name="cand" value="ex" data-href="https://stackoverflow.com/q/45627889/4949005" onclick="alert('You Have Selected Experienced Level \nPlease Click Next To Proceed');" > Experienced<br><br>
<input type="button" id="clickButton" value="Next" />
</form>
做这样的。
- 尝试提交表单,并防止利用
e.preventDefault()
- 然后换用
submit
- 输入按钮类型并添加
data-link
ATTR每个输入。对于有窗有针对性的链接应用形式的默认行为提交后
如何工作
,如果你提交表单。它的发现被检查的单选按钮,数据链路层,然后用 申请
window.location
其.Finaly重定向的URL推崇
$('from').on('submit',function(e){
e.preventDefault();
window.location.href = $('input:checked').attr('data-link');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form>
<input type="radio" name="cand" value="fr" data-link="Fresher"onclick="alert('You Have Selected Fresher Level \nPlease Click Next To Proceed');" > Fresher<br><br>
<input type="radio" name="cand" data-link="Experienced" value="ex" onclick="alert('You Have Selected Experienced Level \nPlease Click Next To Proceed');" > Experienced<br><br>
<input type="submit"/>
</form>
你需要脚本来更新location.href' . Trigger a function on selecting radio button and update the
onclick property using
setAttribute`方法
function updateLink(value) {
if (value === "fr") {
document.getElementById("next").setAttribute('onclick', "location.href='www.google.com'")
} else {
document.getElementById("next").setAttribute('onclick', "location.href='www:wikipedia.com'")
}
}
<form>
<input type="radio" name="cand" value="fr" onclick="updateLink(this.value)"> Fresher<br><br>
<input type="radio" name="cand" value="ex" onclick="updateLink(this.value)"> Experienced<br><br>
<input id="next" type="button" onclick="location.href='www.google.com';" value="Next" />
</form>
此演示仅基于您的代码。使用addEventListener
比使用内联事件处理程序更好。
嘿,我想通过点击提交按钮来打开特定的链接,我试过你的代码,它不像我期望的那样工作,请做一些修改并提供代码 – user8448358
代码中没有'submit'按钮。其次,在演示中,它可能无法打开,因为它会导致加载不安全的代码或浏览器会阻止。你可以尝试打开它从一个HTML文件运行 – brk
非常感谢你 – user8448358
@ user8448358 - 没有probs。它解决了问题吗? – smoksnes