JavaScript的形式 - 阿贾克斯绕过默认行为形式、行为、JavaScript、阿贾克斯

2023-09-10 18:25:38 作者:毀 -

我尝试使用下面的下面的code。我想要的是得到它的单选按钮的值,发送回JavaScript方法。任何变化我尝试似乎失败。

 < HTML>
  < HEAD>
    <风格类型=文本/ CSS>
      身体{背景色:黑色;颜色:白色; }
    < /风格>
    <脚本类型=文/ JavaScript的>
      功能handleClick(事件)
      {

        警报(this.textBox1.value);

        。事件preventDefault(); //禁用正常的表单提交行为
        返回false; // prevent进一步冒泡事件
      }

    < / SCRIPT>
  < /头>
  <身体>
    <表格名称=MyForm的>
      < D​​IV>
 <输入类型=无线电名称=测试1值=奶酪>
 <输入类型=无线电名称=测试2值=葡萄>
 <输入类型=无线电名称=TEST3值=饼干>
      < / DIV>
      <输入类型=提交的onsubmit = JavaScript的:handleClick(值)值=更新/>
    < /形式GT;
  < /身体GT;
< / HTML>
 

解决方案

您code被切断,但我认为,你想要做这样的事情:

 < HTML>
< HEAD>
    <脚本类型=文/ JavaScript的>
        功能alertThing(){
            警报(document.getElementsByName('textBox1的')[0]。价值);
            返回false;
        }
    < / SCRIPT>
< /头>
<身体GT;
    <形式的行动=myPage.php方法=邮报的onsubmit =返回alertThing();>
        <输入类型=文本名称=textBox1的值=你好/>
        <输入类型=提交值=提交表单! />
    < /形式GT;
< /身体GT;
< / HTML>
 
亚特兰大1 0小胜阿贾克斯,小组第二出线KOK,淘汰赛或遇拜仁

这将提醒的价值,但它不会提交表单。希望我帮你!

//莱纳斯Unnebäck

I am trying to use the following code below. What I want is to get the value of which radio button is selected, sent back to the javascript method. Any variation I try seems to fail.

<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>

解决方案

Your code is cut of but I think that you want to do something like this:

<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>

This will alert the value but it will not submit the form. Hope I helped you!

//Linus Unnebäck