2016-08-26 150 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