我的脚本不起作用

时间:2017-01-08 08:46:14

标签: javascript html5 asp.net-mvc-5

我想更改元素所需的消息,但我不知道为什么我的脚本不起作用! src是否正确?!

<head>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery-1.10.2.min.js"></script>

<script>
    $('#form1 input[type=text]').on('change invalid', function () {
        var textfield = $(this).get(0);

        // 'setCustomValidity not only sets the message, but also marks
        // the field as invalid. In order to see whether the field really is
        // invalid, we have to remove the message first
        textfield.setCustomValidity('');

        if (!textfield.validity.valid) {
            textfield.setCustomValidity('Some message... ');
        }
    });
</script>

这是身体。      

    <form id="form1" class="form-basic" action="~/MortgageAndRent/InsertMortgageAndRent/" method="post" >
        <input type="hidden" name="UserId" value="@Model.UserId" />

        <div class="form-row">
            <label>
                <span>Province</span>
                <input type="text" name="AddressViewModel.Province" value="@Model.AddressViewModel.Province" required />
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>City</span>
                <input type="text" name="AddressViewModel.City" value="@Model.AddressViewModel.City" required />
            </label>
        </div>

。 。 。

1 个答案:

答案 0 :(得分:-2)

这个$('#form2 input[type=text]').on('change invalid', function () { var textfield = $(this).get(0);});你已经忘记了};但是在你的html中,你没有声明form2的id,所以我认为这是$('input[type=text]').on('change invalid', function () { var textfield = $(this).get(0);});