简体   繁体   English

HTML表单调用javascript函数

[英]HTML form calling javascript function

This will be a silly question, just trying to figure my way around Javascript functions interacting with HTML forms and can't get it to work. 这将是一个愚蠢的问题,只是试图弄清楚我的Javascript函数与HTML表单交互的方式,并且无法使其正常工作。

Extremely simple, put it in a fiddle 非常简单,放在小提琴中

$('#test').submit(function(){
    var battery = $('#battery').value();
    $('#output').text(battery);
});

<form id="test" action="">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit" onclick="submit()" />
    </fieldset>
</form>

<div id="output">

</div>​​​​

​Always returns an error and I can't figure out why. 总是返回错误,我不知道为什么。 Have played around with and other things, can't get it to work always error. 玩弄了其他东西,总是让它无法正常工作。

The error is probably that .value() isn't a function. 错误可能是.value()不是函数。 You want .val() . 您需要.val()

jQuery's function for returning the value of an input element is val() , not value() . jQuery返回input元素值的函数是val() ,而不是value() Your code should be: 您的代码应为:

$('#test').submit(function(){
    var battery = $('#battery').val();
    $('#output').text(battery);
});

<form id="test" action="">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit" onclick="submit()" />
    </fieldset>
</form>

<div id="output">

</div>​​​​

If you use proper developer tools (for example Firebug in Firefox) then you could easily figure out the problem here as in the console it would have shown you something like : 如果您使用适当的开发人员工具(例如Firefox中的Firebug),则可以轻松地在此找出问题所在,因为在控制台中,它会向您显示以下内容:

Uncaught TypeError: Object [object Object] has no method 'value'

Which means that there is no value method and val() is what should be used instead. 这意味着没有value方法,而应该使用val()代替。

To retrieve values from inputs using jQuery you need to use .val() 要使用jQuery从输入中检索值,您需要使用.val()

Change: 更改:

var battery = $('#battery').value();

to: 至:

var battery = $('#battery').val();

I'm not sure if this is correct, but I think your javascript code looks like php, and there's nothing in the action="" field in the quotes. 我不确定这是否正确,但我认为您的JavaScript代码看起来像php,并且引号中的action=""字段中没有任何内容。 Also, I think that value should be val. 另外,我认为值应该是val。

The correct function is val() no value() in jquery. 正确的函数是jQuery中的val()no value()。

By using the method .submit keep in the "return false" to not run the "action" of the "form" that you have not considered. 通过使用.submit方法,请保留“ return false”以不运行您未考虑的“表单”“操作”

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).on('ready', function() {
        $('#test').submit(function(){
            var battery = $('#battery').val();
            $('#output').text(battery);
            return false;
        });        
    })

</script>

If you are using jQuery, you do not need the onclick="submit()" argument in the input object 如果使用的是jQuery,则在输入对象中不需要onclick =“ submit()”参数

<form id="test" action="#">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit"/>
    </fieldset>
</form>
<div id="output"></div>​​​​

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM