0

在提交此表单之前,我需要进行验证:

    <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>
4

2 回答 2

3

The value fetched from database need to be assigned to variable max and the selects has to be initialized with value 0.

I think it should be

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");
    }

});

Demo: Fiddle

于 2013-06-04T12:38:41.793 回答
0

将 ID 添加到表单:

<form action="" method="POST" name="contract" id="form1">

然后做 :

var max = 3;

$("#form1 select").on('change', function () {
    var selects = 0;
    $("#form1 select").each(function () {
        selects += parseInt(this.value, 10);
    });

    $("#submit").prop('disabled', selects >= max);
});
于 2013-06-04T12:42:07.320 回答