使用Magento 1.9 ->addFieldDependence
方法,我的相关字段值会动态更改,因此不会触发magento字段依赖方法。
我试过jquery $j("#meter_type_code").trigger("change");
强制onchange事件并触发Magento依赖函数无济于事。
我的代码的相关摘录:
<script>
var metertype = <?php echo $meter_type; ?>; //$metertype is an array
</script>
$fieldset->addField('meter_type_code', 'text', array( //set to text for testing will be hidden
'name' => 'machine.meter_type_code',
$fieldset->addField('meter_type', 'select', array(
'label' => 'Meter Type',
'class' => 'required-entry',
'required' => true,
'name' => 'machine.meter_type',
'onchange' => 'changeFieldValue(metertype[this.value]);trigger();',
'values' => Mage::getModel('machinemanager/machine_metertype')->getMeterTypes($attributeSetName),
'disabled' => false,
'readonly' => false,
'after_element_html' => '</br><small>Time in service</small>',
'tabindex' => 1
))
->setAfterElementHtml('
<script>
function changeFieldValue(value) {
document.getElementById("meter_type_code").value = value;
}
function trigger(value) {
//this is where I tried various methods to trigger the event none of which had effect to fire the Magento field dependence
}
</script>
');
$this->setChild('form_after', $this->getLayout()
->createBlock('adminhtml/widget_form_element_dependence')
->addFieldMap('meter_type_code','meter_type_code')
->addFieldMap('meter_type','meter_type')
->addFieldMap('time', 'time')
->addFieldMap('time_adjust', 'time_adjust')
->addFieldMap('cycles', 'cycles')
->addFieldMap('cycles_adjust', 'cycles_adjust')
->addFieldDependence('time', 'meter_type_code', array('t','tc'))
->addFieldDependence('time_adjust', 'meter_type_code', array('t','tc'))
->addFieldDependence('cycles', 'meter_type_code', array('c','tc'))
->addFieldDependence('cycles_adjust', 'meter_type_code', array('c','tc'))
);
当我使用jquery成功强制onchange事件时,在我实际重新输入meter_type_code的值并导致真正的键盘操作之前没有任何反应。这触发了magento javascript使依赖字段出现并完美消失。
当有人从另一个字段动态更新依赖关系的目标字段时,有人能告诉我如何触发addFieldDependence吗?
谢谢。
答案 0 :(得分:0)
问题解决了! Thanks to Mirasvit Extensions
此触发器有效:
function trigger(value) {
// $j("#meter_type_code").trigger("change");
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
document.getElementById("meter_type_code").dispatchEvent(evt);
}
我注释掉了触发更改事件的JQuery方法,但没有导致Magento依赖方法工作。我对任何评论感兴趣,为什么JQuery触发器在这种情况下不起作用。