在提交此表单之前,我需要进行验证:
<form action="<?php echo $editFormAction; ?>" method="POST" id="form1" name="contract">
<table class="myp-table">
<tr>
<td>Value Test</td>
</tr>
<?php do { ?>
<tr>
<td><select name="playerContract[]">
<option value="0" <?php if (!(strcmp(0, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>0</option>
<option value="1" <?php if (!(strcmp(1, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>1</option>
<option value="2" <?php if (!(strcmp(2, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>2</option>
<option value="3" <?php if (!(strcmp(3, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>3</option>
</select></td>
</tr>
<?php } while ($row_datacontract = mysql_fetch_assoc($datacontract)); ?>
<tr>
<td><input class="linkbuttonmp" name="contract" type="submit" value="Invio" /></td></tr>
</table>
</form>
用户选择的值的总和不能超过默认值,在我的例子中是从我的数据库中检索到的值。
我以这种方式尝试使用jquery:
var max = 3;
$("#form1 select").change(function () {
var selects = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
不幸的是,它不起作用。
你能帮助我吗?
更新
对不起,我犯了一个错误:以下是我实际尝试过的脚本:
<script type="text/javascript">
var max = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").change(function () {
var selects = 0;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
</script>