如何在cakephp中使用jquery添加或删除文本框中的readonly属性?

2q5ifsrm  于 2023-05-17  发布在  PHP
关注(0)|答案(3)|浏览(202)

下面是我的cakephp生成的HTMLradio boxtext box脚本:

<input type="radio" id="need_staff_on_site" name="data[CaterRequest][need_staff_on_site]" value="yes" class="staff_on_site"><span>Yes</span>

<input type="radio" id="need_staff_on_site" name="data[CaterRequest][need_staff_on_site]" class="staff_on_site" value="no"><span>No</span>

How many staff?<input type="text" maxlength="3" id="no_of_staff" name="data[CaterRequest][staff_needed]" class="txtboxSml2" readonly="readonly">

jquery脚本:

$(document).ready(function(){
   $('.staff_on_site').click(function(){
   $arr=$(this).val();
   if($arr == "yes"){ $("#no_of_staff").removeAttr("readonly"); }
   if($arr == "no"){ $("#no_of_staff").attr("readonly", "readonly"); }
  });
});

Demo jsfiddle Link

rxztt3cl

rxztt3cl1#

在你的Case中,你可以编写以下jquery代码:

$(document).ready(function(){

   $('.staff_on_site').click(function(){

     var rBtnVal = $(this).val();

     if(rBtnVal == "yes"){
         $("#no_of_staff").attr("readonly", false); 
     }
     else{ 
         $("#no_of_staff").attr("readonly", true); 
     }
   });
});

这是小提琴:http://jsfiddle.net/P4QWx/3/

6yt4nkrj

6yt4nkrj2#

你也可以使用prop。检查下面的代码。

$(document).ready(function(){

   $('.staff_on_site').click(function(){

     var rBtnVal = $(this).val();

     if(rBtnVal == "yes"){
         $("#no_of_staff").prop("readonly", false); 
     }
     else{ 
         $("#no_of_staff").prop("readonly", true); 
     }
   });
});
wribegjk

wribegjk3#

你可以像这样删除attr

$('.editname').removeAttr('readonly');

你可以像这样添加readonly属性

$('.editname').attr('readonly', true);

相关问题