2017-01-08 74 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/}和)結束。 –