Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将HTML表单值传递给javascript_Javascript_Html - Fatal编程技术网

将HTML表单值传递给javascript

将HTML表单值传递给javascript,javascript,html,Javascript,Html,我试图创建一个包含一个文本框和一个按钮的页面,当点击按钮时,它会将值发送到javascript,然后写入内容。我花了相当长的时间寻找,我看不出我做错了什么。这是脚本和页面 HTML: <!doctype html> <html> <head> <meta charset="utf-8"> <title>RDP</title> <link re

我试图创建一个包含一个文本框和一个按钮的页面,当点击按钮时,它会将值发送到javascript,然后写入内容。我花了相当长的时间寻找,我看不出我做错了什么。这是脚本和页面

HTML:

    <!doctype html>
      <html>
      <head>
        <meta charset="utf-8">
        <title>RDP</title>
       <link rel = "stylesheet" type = "text/css" 
             href = "css/style.css">
    </head>

    <body>  
        <div class='center'>
          <div class='header'>
              <h1>Home</h1>
          <h2></h2>
        </div>
          <div class='sidebar'>
            <ul>
                <li><a href='index.html'>Home</a></li>
                <li><a href='divevideo.html'>Video</a></li>
                <li>Dive Calculator</li>
            <li><a href='images.html'>Images</a></li>
         </ul>
      </div>
        <div class='main'>
         <p><form>Depth:<input name="Depth" type="text" id="Depth"value="35" size="3" maxlength="3">
            <input name="Calculate" type="button" id="calculate" onClick="DepthEntered()"     value="Calculate"></form></p>
         <p id='divetimes'></p>
         <script src="jsfiles/nodecom.js"></script>
      </div>
      <div class='footer'>
      </div>
    </div>
    </body>

   </html>

RDP
家
  • 潜水计算器
深度:

Javascript:

// JavaScript Document
function DepthEntered()
{
    var depth = document.getElementById("Depth").value;
    var nodetime;
    var sstime;

    //depth = window.prompt('What was your depth?')

    //depth = 62 //temperary test variable. remove to return to dynamic.

    if( depth >= 35 )
    {
        nodetime = 205
        sstime = 152
    }
    if( depth > 35 && depth >= 40 )
    {
        nodetime = 140
        sstime = 111
    }
    if( depth > 40 && depth >= 50 )
    {
        nodetime = 80
        sstime = 67
    }
    if( depth > 50 && depth >= 60 )
    {
        nodetime = 55
        sstime = 49
    }
    if( depth > 60 && depth >= 70 )
    {
        nodetime = 40
        sstime = 35
    }
    if( depth > 70 && depth >= 80 )
    {
        nodetime = 30
        sstime = 26
    }
    if( depth > 90 && depth >= 100 )
    {
        nodetime = 25
        sstime = 22
    }
    if( depth > 100 && depth >= 110 )
    {
        nodetime = 20
        sstime = 0
    }
    if( depth > 110 && depth >= 120 )
    {
        nodetime = 16
        sstime = 0
    }
    if( depth > 120 && depth >= 130 )
    {
        nodetime = 10
        sstime = 0
    }
    if( depth > 130 && depth >= 140 )
    {
        nodetime = 8
        sstime = 0
    }
    if( depth > 140 )
    {
        document.getElementById("divetimes").innerHTML = ('<p>There is no safe no decompression time for this depth.</p>');
    }
    else
    {
        document.getElementById("divetimes").innerHTML = ('<p>At that depth your no decompression limit is <em class = myclass2>' + nodetime + '</em> minutes. You need to make a saftey if your down at least <em class = myclass3>' + sstime + '</em> minutes.</p>'); 
    };
}
//JavaScript文档
函数去噪()
{
var depth=document.getElementById(“depth”).value;
变量节点时间;
var-sstime;
//深度=window.prompt('您的深度是多少?')
//深度=62//临时测试变量。删除以返回动态。
如果(深度>=35)
{
nodetime=205
sstime=152
}
如果(深度>35和深度>40)
{
nodetime=140
sstime=111
}
如果(深度>40和深度>50)
{
nodetime=80
sstime=67
}
如果(深度>50和深度>60)
{
nodetime=55
sstime=49
}
如果(深度>60和深度>70)
{
nodetime=40
sstime=35
}
如果(深度>70和深度>80)
{
nodetime=30
sstime=26
}
如果(深度>90和深度>100)
{
nodetime=25
sstime=22
}
如果(深度>100和深度>110)
{
nodetime=20
sstime=0
}
如果(深度>110和深度>120)
{
nodetime=16
sstime=0
}
如果(深度>120和深度>130)
{
nodetime=10
sstime=0
}
如果(深度>130和深度>140)
{
nodetime=8
sstime=0
}
如果(深度>140)
{
document.getElementById(“divetimes”).innerHTML=(“此深度没有安全的解压缩时间。

”); } 其他的 { document.getElementById(“divetime”).innerHTML=(“在该深度,您的无解压缩限制为“+nodetime+”分钟。如果您的下载时间至少为“+sstime+”分钟,则需要进行安全操作。

”); }; }
另外,如何使其仅能输入数字?

使用jQuery:

jQuery(function($) {
    $(document).on('keydown', '.numberOnly', function(e) {
        if(!((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 97 && e.keyCode <= 106) || e.keyCode == 8)) {
            e.preventDefault();
        }
    });
});
jQuery(函数($){
$(文档).on('keydown','.numberOnly',函数(e){

如果(!((e.keyCode>=48&&e.keyCode=97&&e.keyCode请确保您的JavaScript位于HTML下方,因此
取消设置()
是在窗口范围中定义的。请参见此提琴:。您的代码按原样工作!

对于HTML5,输入type='number'可确保输入的输入类型仅为数字。在输入标记末尾添加'required'可确保用户在提交之前输入数字。适用于大多数浏览器。例如

另外,为了使其工作,您可以尝试在javascript中将其解析为整数。 var d=parseInt(深度)


希望有帮助!:)

你应该从javascript中的开关和对象作为映射开始,用更好的东西替换那些if!现在返回的值是多少?另外,为了让你的生活更轻松,删除或注释掉除不起作用的部分以外的所有东西。然后,当该部分起作用时,你可以处理所有复杂的东西。这会让你更轻松更简单!你的HTML中有一个错误。输入标记没有关闭。最后,Chrome和Firefox内置了强大的调试工具,你可以通过按F12来访问它们。如何读取它们,你可以节省自己的时间作为回报。但现在你不能使用退格。我不能按退格。也不能从NUMLOCK部分输入数字.添加了backspace和numblock支持谢谢这回答了我的问题!是parseInt部分打动了我的头脑。