js 函数在 cakephp 中使用 onClick onLoad
js function to work onLoad with onClick in cakephp
在 cakephp 中,当我加载页面和 select enable/disable 文本字段的选项时,下面的代码工作正常。
问题是,当我在其他地方单击 cakephp 按钮刷新页面时,之前设置为禁用的文本字段再次启用。
如何在页面刷新时处理禁用的文本字段?
window.onload=function(){
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
}
function myFunc(val) {
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
// alert( document.getElementById("dateRange").value);
}
...
echo $this->Form->input('startDate',array('id'=>'startdatebox','label' => 'Start Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $datestart));
echo $this->Form->input('endDate',array('id'=>'enddatebox','label' => 'End Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $dateend));
$selectoption=array(1=>'Today',0=>'Fortnight',2=>'Monthly',3=>'Custom Range');
echo $this -> Form -> input('dateRange',
array('id'=>'dateRange2','label' => '<h6>Date Range</h6>','type' => 'radio',
'value' =>$dateSelect,'options' => $selectoption, 'onclick'=> 'myFunc(this.value)'));
使用jQuery的解决方案
<script type="text/javascript">
$(document).ready(function() {
var val = $("input[type=radio][name='dateRange2']:checked").val();
if(val!=3) {
$('#startdatebox,enddatebox').prop('disabled', true);
}
});
而不是
window.onload=function(){
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
}
别忘了加载 jquery library。
在 cakephp 中,当我加载页面和 select enable/disable 文本字段的选项时,下面的代码工作正常。
问题是,当我在其他地方单击 cakephp 按钮刷新页面时,之前设置为禁用的文本字段再次启用。
如何在页面刷新时处理禁用的文本字段?
window.onload=function(){
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
}
function myFunc(val) {
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
// alert( document.getElementById("dateRange").value);
}
...
echo $this->Form->input('startDate',array('id'=>'startdatebox','label' => 'Start Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $datestart));
echo $this->Form->input('endDate',array('id'=>'enddatebox','label' => 'End Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $dateend));
$selectoption=array(1=>'Today',0=>'Fortnight',2=>'Monthly',3=>'Custom Range');
echo $this -> Form -> input('dateRange',
array('id'=>'dateRange2','label' => '<h6>Date Range</h6>','type' => 'radio',
'value' =>$dateSelect,'options' => $selectoption, 'onclick'=> 'myFunc(this.value)'));
使用jQuery的解决方案
<script type="text/javascript">
$(document).ready(function() {
var val = $("input[type=radio][name='dateRange2']:checked").val();
if(val!=3) {
$('#startdatebox,enddatebox').prop('disabled', true);
}
});
而不是
window.onload=function(){
if(val != 3) {
document.getElementById("startdatebox").disabled = true;
document.getElementById("enddatebox").disabled = true;
} else {
document.getElementById("startdatebox").disabled = false;
document.getElementById("enddatebox").disabled = false;
}
}
别忘了加载 jquery library。