2017-01-08 70 views
-1

我想更改元素所需的信息,但我不知道为什么我的脚本不起作用! 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> 

。 。 。

+0

你必须先学习如何调试你的代码。检查此链接http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code –

+0

你的'#form2 input [type = text]'元素在你运行时不存在你的代码设置事件处理程序。查看链接问题的答案。 (你还需要在输入周围使用'id =“form2”'或者改变选择器的元素。) –

回答

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

+0

你也忘了在末尾关闭')':) –

+0

是的,刚刚看到感谢你说出来了,在手机上写代码不是很好:) – NotanNimda

+0

@NotanNimda。非常感谢您的回复。在这个链接http://jsfiddle.net/zpkKv/2/}和)结束。 –