Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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
使用JQuery/Javascript在保存在线表单之前对其进行预处理_Javascript_Jquery_Html_Forms_Chronoforms - Fatal编程技术网

使用JQuery/Javascript在保存在线表单之前对其进行预处理

使用JQuery/Javascript在保存在线表单之前对其进行预处理,javascript,jquery,html,forms,chronoforms,Javascript,Jquery,Html,Forms,Chronoforms,在提交在线表单输入并将其存储到数据库之前,我需要一些帮助来预处理这些输入。这个表单实际上是在Joomla 3.1中的ChronoForms组件的帮助下完成的。表单是使用HTML生成的,处理是使用JQuery/JavaScript完成的,在“OnLoad”事件中使用“Load JS”操作(对于熟悉ChronoForms的人) 简化表格如下: <form name="online_form" id="online_form"> <select name="period_ye

在提交在线表单输入并将其存储到数据库之前,我需要一些帮助来预处理这些输入。这个表单实际上是在Joomla 3.1中的ChronoForms组件的帮助下完成的。表单是使用HTML生成的,处理是使用JQuery/JavaScript完成的,在“OnLoad”事件中使用“Load JS”操作(对于熟悉ChronoForms的人)

简化表格如下:

<form name="online_form" id="online_form">
    <select name="period_year" id="period_year">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <select name="period_month" id="period_month">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <input type="hidden" name="period" id="period"/>
    <input type="submit" id="submit_form"/>
</form>
另一种选择是对select元素使用onchange属性,如下所示,但它似乎对我也不起作用:

....
<select name="period_year" id="period_year" onchange="process()">...</select>
<select name="period_month" id="period_month" onchange="process()">...</select>
....
我做错了什么?或者还有其他正确的方法吗?

您需要使用
#
更改为
val()
。值与
DOM
对象一起使用,选择器将返回
jQuery
对象。如果要使用value,请使用
[0]
索引器将jQuery对象转换为DOM对象

$('#online_form').addEventListener('submit', 
    function() { 
        $('#period').val(period_year+' year and '+period_month+' months');
        //$('period')[0].value=period_year+' year and '+period_month+' months';
    }
);
需要
#
选择具有id的元素

$('#online_form').submit(function() {   
//-^---here
     $('#period').val(period_year+' year and '+period_month+' months');
     //-^---here

)};

我尝试添加#,但使用Google的inspect元素,它抛出了一个错误:“未捕获的TypeError:无法调用null的方法'addEventListener'”我尝试添加#,但使用Google的inspect元素,它抛出了一个错误:“未捕获的TypeError:无法调用null的方法'addEventListener'@ImpStudent,阅读将
addEventListener
替换为的答案。它可能是写的,更像你的代码,
$('#online_form')。在('submit',function(){…})上
$('#online_form').addEventListener('submit', 
    function() { 
        $('#period').val(period_year+' year and '+period_month+' months');
        //$('period')[0].value=period_year+' year and '+period_month+' months';
    }
);
$('#online_form').submit(function() {   
//-^---here
     $('#period').val(period_year+' year and '+period_month+' months');
     //-^---here

)};