2016-08-26 149 views
0

HTML:復位禁止輸入字段的值

<template name="Dep_Con"> 
<input disabled={{SBD_Dep_Con}} class="input" value="" type="text"/> 
</template> 

JS:

Template.registerHelper("SBD_Dep_Con", function() { 
    var returnval = "n"; 
    const ans = Session.get('chosen') 
    const product = ProductList.findOne({ product: ans}); 
    console.log("product.dep_con:" + product.department_contact) 
    if(product.department_contact == "N/A") {return true} 
    else {return false} 
}); 

我已經成功地啓用/禁用HTML中的inputfield(文本)使用上面的代碼依賴於另一個下拉框的值。

問題是當輸入字段被啓用時被填充,然後被禁用,填充的值仍然存在。當「禁用」狀態改變時,是否有辦法重置輸入字段的值?或者我看錯了方向(即,表單不能從禁用的輸入字段中檢索值)?

回答

1

您可以在使用jquery禁用輸入字段之前清除輸入字段。我稍微修改了你的代碼。

<template name="Dep_Con"> 
    <input disabled={{SBD_Dep_Con}} class="input" id="input_field" value="" type="text"/> 
</template> 

JS文件

Template.registerHelper("SBD_Dep_Con", function() { 
    var returnval = "n"; 
    const ans = Session.get('chosen'); 
    const product = ProductList.findOne({ product: ans}); 
    console.log("product.dep_con:" + product.department_contact) 
    if(product.department_contact == "N/A") { 
     $('#input_field').val(''); //Add this corresponding to id. 
     return true } 
    else { 
    return false} 
}); 
+0

奏效,謝謝!我以前也有同樣的事情,沒有工作認爲它不是這樣,只是意識到我錯過了'#' – Alex