在提交和存储在数据库中之前,我需要一些预处理在线表单输入的帮助。这个表单实际上是在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>
我想要做的是当用户提交表单时,将period_year和period_month的值组合成文本字符串“x year和y months”,并将其作为period的值。数据库最终将存储句点而不是period_year和period_month。
我尝试在提交表单时使用事件监听器,但它似乎不起作用:
window.addEvent('domready', function() {
$('online_form').addEventListener('submit',
function() {
var period_year = $('period_year').value;
var period_month = $('period_month').value;
$('period').value=period_year+' year and '+period_month+' months';
}
);
}
另一个选择是在select元素上使用onchange属性,如下所示,但它似乎也不适用于我:
....
<select name="period_year" id="period_year" onchange="process()">...</select>
<select name="period_month" id="period_month" onchange="process()">...</select>
....
对于头:
function process() {
$('period').value=period_year+' year and '+period_month+' months';
};
我做错了什么?或者还有其他正确的方法吗?
答案 0 :(得分:2)
需要#
来选择ID为
$('#online_form').submit(function() {
//-^---here
$('#period').val(period_year+' year and '+period_month+' months');
//-^---here
)};
答案 1 :(得分:1)
您需要#
使用value
同时将val()
更改为DOM
。 value与jQuery
对象一起使用,selector将返回[0]
个对象。如果要使用value,请使用$('#online_form').addEventListener('submit',
function() {
$('#period').val(period_year+' year and '+period_month+' months');
//$('period')[0].value=period_year+' year and '+period_month+' months';
}
);
索引器将jQuery对象转换为DOM对象。
{{1}}