根据列表中选择的内容设置复选框的值 [英] Setting the value of a checkbox based on what is selected in a list

查看:112
本文介绍了根据列表中选择的内容设置复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CMS系统的限制范围内工作,该系统定义了可用于在PHP中的应用程序中使用的表单的某些字段。



函数具有签名:

 函数inputBasicList($ id,$ value =,$ list = array = NULL,$ displayLabel = true)



我这样使用:

  $ theinput = new inputBasicList(type,$ therecord [paymenttype],array(Cash=>cash,Credit >credit),Payment Type); 

同样,有一个复选框,其签名为:

 函数inputCheckbox($ id,$ value = false,$ displayName = NULL,$ disabled = false,$ displayLabel = true)

我会这样使用

  $ theinput = new inputCheckbox(paid,$ therecord [paid],Paid); 

我想做的是,如果列表设置为credit而不是默认的cash,以自动将复选框设置为true / checked。



我不认为CMS系统允许使用任何内置函数来执行此操作,并且谨慎添加任何javascript。



只有PHP可能吗?



否则,javascript会有多复杂

编辑:



从phpBMS表单生成的HTML

 < p class =big>< label for =typeclass =important>付款方式< /标签> 
< br />
< select name =typeid =typeclass =important>
< option value =cash>现金< / option>
< option value =credit>信用< / option>
< / select>
< / p>
< p class =big>
< input type =checkboxid =paidname =paidvalue =1class =radiochecks/>
< label id =paidLabelfor =paid> Paid< / label>
< / p>


解决方案

因为PHP在您的服务器上运行。您需要一些在客户端上运行的代码。



我相信第一个参数 $ id id 属性的元素?如果我错了正确我。如果是这样,您可以使用jQuery JavaScript库执行以下操作:

  jQuery(function($){
$ '#type')。change(function(){
if($(this).val()==credit){
$('#paid')。attr('checked' ,'checked');
} else {
$('#paid')。removeAttr('checked');
}
});
} ;

UDPATE
BMS正在使用Mootools,JavaScript应该像这样在mootools工作:

  window.addEvent('domready',function(){
$('类型')。addEvent('change',function(){
if($(this).get('value')=='credit'){
$('paid')。set ('checked','checked');
} else {
$('paid')。removeProperty('checked');
}
});
});

我建议使用mootools版本的这段代码,但只是为了你的兴趣,如果你想安装jQuery,您可以将 jquery.js 添加到 phpbms / common / javascript 。然后,您可以编辑 phpbms / header.php ,以包括以下内容:



最后 $ tempjsarray [] 后添加:

  $ tempjsarray [] =common / javascript / jquery.js; 

然后在 $ phpbms-> showJsIncludes(); 你需要包括这个,所以jQuery工作没有问题,除了mootools:

  echo'< script type =text /javascript\">jQuery.noConflict();</script>'; 






如果这不起作用,什么是html输出看起来像。


I am working in the confines of a CMS system, which defines certain fields which can be used to make forms for use within the application in PHP.

The list function has the signature:

function inputBasicList ($id,$value = "",$list = array(), $displayName = NULL, $displayLabel = true)

I use it thusly:

$theinput = new inputBasicList("type",$therecord["paymenttype"],array("Cash"=>"cash","Credit"=>"credit"), "Payment Type");

Likewise, there is a checkbox, which has the signature:

function inputCheckbox($id,$value = false, $displayName = NULL, $disabled = false, $displayLabel = true)

I use it thusly

$theinput = new inputCheckbox("paid", $therecord["paid"], "Paid");

What I would like to do, is if the list is set to credit instead of the default cash, to automatically set the checkbox to true/checked.

I don´t think the CMS system allows a way to do this using any built in functions, and am wary of adding any javascript.

Is such a thing possible with just PHP?

Otherwise, how complicated would the javascript have to be to do such a thing?

edit:

The generated HTML from the phpBMS forms

<p class="big"><label for="type" class="important">Payment Type</label>
<br />
<select name="type" id="type"  class="important" > 
<option value="cash"  >Cash</option>
<option value="credit"  >Credit</option>
</select>
</p>
<p class="big">
<input type="checkbox" id="paid" name="paid" value="1" class="radiochecks"  /> 
<label id="paidLabel" for="paid" >Paid</label>
</p>

解决方案

It's not possible to do such thing with PHP only, because PHP is run on your server. You need some code that is run on the client.

I believe that the first paramater $id is used as id attribute for the elements? If I'm wrong correct me. If so you can do the following using the jQuery JavaScript Library:

jQuery(function($){
  $('#type').change(function(){
    if ($(this).val() == "credit") {
      $('#paid').attr('checked','checked');
    } else {
      $('#paid').removeAttr('checked');
    }
  });
});

UDPATE BMS is using Mootools, the JavaScript should like like this to work in mootools:

window.addEvent('domready', function(){
  $('type').addEvent('change',function(){
    if($(this).get('value') == 'credit') {
      $('paid').set('checked','checked');
    } else {
      $('paid').removeProperty('checked');
    }
  });
});

I would recommend using the mootools version of this snippet, but just for your interest, if you want to install jQuery, you can add the jquery.js into phpbms/common/javascript. Then you can edit phpbms/header.php to include this:

after the last $tempjsarray[] add:

$tempjsarray[] = "common/javascript/jquery.js";

then after $phpbms->showJsIncludes(); you need to include this, so jQuery works without problems besides mootools:

echo '<script type="text/javascript">jQuery.noConflict();</script>';


If this doesn't work, you should post what the html output looks like.

这篇关于根据列表中选择的内容设置复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆