gpt4 book ai didi

javascript 表单 - 绕过 ajax 的默认行为

转载 作者:行者123 更新时间:2023-11-30 18:59:00 25 4
gpt4 key购买 nike

我正在尝试使用下面的代码。我想要的是获取选择哪个单选按钮的值,并将其发送回 javascript 方法。我尝试的任何变体似乎都失败了。

<html>
<head>
<style type="text/css">
body { background-color: black; color: white; }
</style>
<script type="text/javascript">
function handleClick(event)
{

alert(this.textBox1.value);

event.preventDefault(); // disable normal form submit behavior
return false; // prevent further bubbling of event
}

</script>
</head>
<body">
<form name="myform">
<div>
<input type="radio" name="test1" value="Cheese">
<input type="radio" name="test2" value="Grapes">
<input type="radio" name="test3" value="Crackers">
</div>
<input type="submit" onsubmit=JavaScript:handleClick(value) value="Update"/>
</form>
</body>
</html>

最佳答案

你的代码被删减了,但我认为你想做这样的事情:

<html>
<head>
<script type="text/javascript">
function alertThing() {
alert(document.getElementsByName('textBox1')[0].value);
return false;
}
</script>
</head>
<body>
<form action="myPage.php" method="post" onsubmit="return alertThing();">
<input type="text" name="textBox1" value="hello" />
<input type="submit" value="Submit the form!" />
</form>
</body>
</html>

这将提醒该值,但不会提交表单。希望我能帮到你!

//林纳斯乌尼贝克

关于javascript 表单 - 绕过 ajax 的默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/662630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com