2012-03-29 242 views
6

我有一個對話框,它執行依賴於三個輸入字段的計算。當它們中的任何一個被改變時,它檢查它們是否全部被填滿,以及它們是否是過程並給出其響應。jQuery .change()事件在IE中未觸發

它在FF,Chrome,Opera等工作絕對正常,但在任何版本的IE中它只是停止工作。我的其餘jQuery工作正常,但我無法確切地確定問題出在哪裏。我認爲這可能是與.change()事件或多個事件綁定,但我真的不知道。

這裏是有問題的代碼:

var loan = $("#ltv-loan-amount"); 
var mortgage = $("#ltv-mortgage"); 
var property = $("#ltv-property"); 

$("#ltv-dialog input").change(function() { 
    if(loan.val() && mortgage.val() && property.val()) 
    { 
     var ltv = parseInt(((parseInt(loan.val()) + parseInt(mortgage.val()))/parseInt(property.val()))* 1000); 
     ltv /= 10; 
     if(ltv > 0 && ltv < 85) 
     { 
      $("#ltv-result").html("<span id=\"ltv-form-result\">" + ltv + "%</span></p><p><a id=\"ltv-link\" href=\"#sidebar-head\">Congratulations, your LTV has passed. Please now proceed with your application opposite.</a>"); 
      $("#amount").val(loan.val()); 
      $("#mortgage_balance").val(mortgage.val()); 
      $("#prop_value").val(property.val()); 
      $("#ltv-link").click(function() { 
       $("#ltv-dialog").dialog("close"); 
      }); 
     } 
     else if (ltv > 84) 
     { 
      $("#ltv-result").html("<span id=\"ltv-form-result\">" + ltv + "%</span></p><p>Unfortunately your LTV is greater than 85%. You may still qualify for a loan if you reduce the loan amount."); 
     } 
     else 
     { 
      $("#ltv-result").html("</p><p>Please check your input above as you currently have a negative ltv."); 
     } 
    } 
}); 

,問題中的HTML:

<div id="ltv-dialog" title="Do You Qualify for a Loan?"> 
    <p>Please enter the fields below and your LTV will be calculated automatically.</p> 
    <div id="ltv-form"> 
     <div class="ltv-row"> 
      <label for="ltv-loan-amount">Loan Amount:</label> 
      <input type="text" name="ltv-loan-amount" id="ltv-loan-amount" class="p000" /> 
     </div> 
     <div class="ltv-row"> 
      <label for="ltv-mortgage">Mortgage Value:</label> 
      <input type="text" name="ltv-mortgage" id="ltv-mortgage" class="p000" /> 
     </div> 
     <div class="ltv-row"> 
      <label for="ltv-property">Estimated Property Value:</label> 
      <input type="text" name="ltv-property" id="ltv-property" class="p000" /> 
     </div> 
    </div> 
    <div class="ltv-row"> 
     <p>Loan to Value % (LTV): <span id="ltv-result"></span></p> 
    </div> 
</div> 

的更新

變化事件似乎是射擊,但文字只有當框從值更改爲空。

+0

你檢查錯誤控制檯? – 2012-03-29 08:15:22

+1

我認爲以下職位將幫助你: [http://stackoverflow.com/questions/208471/getting-jquery-to-recognise-change-in-ie][1] [1]:http://stackoverflow.com/questions/208471/getting-jquery-to-recognise-change-in-ie – pascalvgemert 2012-03-29 08:15:33

+2

請顯示相關的HTML。如果您使用單選按鈕或複選框,那麼您可能想要聽'.click()'而不是'.change()'。 – jfriend00 2012-03-29 08:16:13

回答

8

當你的目標文本輸入的元素,你有沒有嘗試過使用不同的事件?因此,而不是change使用,例如,keyup$("#ltv-dialog input").keyup(function() {。這將在每次按鍵後觸發。

+0

這個工作完美 - 甚至改進了我試圖達到的目標。 – unfrev 2012-03-29 09:00:04

+0

謝謝,很好的解決方案。 – Kyslik 2013-07-29 09:37:36

3

使用jQuery .on()也可能解決您的問題。使用這樣的:

jQuery("#ltv-dialog").on("change", "input", function(){ 
    alert("It works !"); 
    //your javascript/jQuery goes here 
    } 
); 

jsfiddle

0

使用Live功能,它通常使得它的工作...

$("#YOURTHING").live("change", function(){ 

// CODE HERE 

}); 
+1

.live(和.delegate)自jQuery 1.7以來已棄用。我們應該使用.bind或.on將事件附加到選擇器。 .bind不會將事件附加到正被動態添加到DOM的元素,即使元素被動態添加到DOM,.on也會附加事件。 – 2014-08-06 16:43:06

0

請使用,而不是改變/每次點擊,這是工作在IE罰款,甚至第一次和其他瀏覽器

不工作第一次嘗試

$("#checkboxid").change(function() { 

}); 

即使是第一次也能正常工作

$("#checkboxid").each(function() { 

}); 
0

我有同樣的問題:修正以下爲我工作:

相反的:$("#ltv-dialog input").change(function() {

我用:$('#ltv-dialog').on('input', function() {