[英]using dropdown label to validation other dropdown value
I have two dropdowns: 我有两个下拉菜单:
<select name="first_value" id="first_value">
<option value="<?php print"$sum_jan" ?>">January</option>
<option value="<?php print"$sum_feb" ?>">February</option>
<option value="<?php print"$sum_mar" ?>">March</option>
<option value="<?php print"$sum_apr" ?>">April</option>
<option value="<?php print"$sum_may" ?>">May</option>
<option value="<?php print"$sum_june" ?>">June</option>
<option value="<?php print"$sum_july" ?>">July</option>
<option value="<?php print"$sum_aug" ?>">August</option>
<option value="<?php print"$sum_sept" ?>">September</option>
<option value="<?php print"$sum_oct" ?>">October</option>
<option value="<?php print"$sum_nov" ?>">November</option>
<option value="<?php print"$sum_dec" ?>">December</option>
</select>
and 和
<select name="month" id="month">
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
The JavaScript for these dropdowns: 这些下拉菜单的JavaScript:
function validateForm() {
var a = document.forms["myForm"]["first_value"].value;
var b = document.forms["myForm"]["month"].value;
var c = document.forms["myForm"]["sec_value"].value;
if (a != b) {
alert("Month isn't Same");
$("#month").focus();
return false;
} else {
if (c == "") {
alert("Recently sold item empty");
$("#sec_value").focus();
return false;
}
}
}
I've been trying adding labels to first_value (ie label="January"
and so on), or both, and change .value
to .label
, or using .option
in validation script, but still doesn't work. 我一直在试图增加标签FIRST_VALUE(即
label="January"
等),或两者兼而有之,并更改.value
到.label
,或使用.option
在验证脚本,但仍然无法正常工作。 It just passes without error, even when first_value.label
and month.label
do not match. 即使
first_value.label
和month.label
不匹配,它也会通过而不会出现错误。
I need first_value
value in my form to post the sum_value
and month
to post the month. 我需要
first_value
值来发布sum_value
和month
来发布月份。
Later in the action form I want it become like this: 稍后在动作表单中,我希望它变成这样:
$first_value=$_post['first_value'];
$sec_value=$_post['sec_value']
$final_value=$fist_value+$sec_value
$month = month
If I'm not using validation, it's already done I think, nothing to do more, but I still need it match the month that user wants to choose, for updating data. 如果我不使用验证,那么我认为它已经完成了,没什么可做的,但是我仍然需要它与用户想要选择的月份相匹配,以更新数据。
first_value
is sum all data in the month and when the updating process works, it's really updating the correct month, in case someone picks a wrong month. first_value
是月份中所有数据的总和,当更新过程正常工作时,它实际上是在更新正确的月份,以防万一有人选择了错误的月份。
Sample html showing how you can call validate function, also prevent default form submit. 样本html显示了如何调用validate函数,还阻止了默认表单提交。
NOTE: You must do changes based on your requirement, I am trying to explain how you can call validate function 注意:您必须根据您的要求进行更改,我试图解释如何调用验证函数
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function validateForm(e) {
e.preventDefault();//change
var a = document.forms["myForm"]["first_value"].value;
var b = document.forms["myForm"]["month"].value;
var c = document.forms["myForm"]["sec_value"].value;
if (a != b) {
alert("Month isn't Same");
$("#month").focus();
return false;
}else{
if (c == "") {
alert("Recently sold item empty");
$("#sec_value").focus();
return false;
}
}
}
</script>
$(document).ready(function(){
$("form").submit(function(e){
validateForm(e);// your function
});
});
</script>
</head>
<body>
<form id="myForm" action=""><!--your form, you should add your elements in form-->
<!--your select boxes should be placed here-->
<input type="submit" value="Submit">
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.