· 提交含有CheckBox的表单
问题描述:在Form中如果含有 type
为 checkbox
的 input
标签时,提交表单后会发现,只有选中的最后一个CheckBox值被提交,之前选中的项消失了。。。
解决方法:利用js循环某组input标签,把 checked = true
的input的值取出;然后,增加一个新的 Form
, 将选中的值赋值给它;最后,提交新的Form
表。
<body>
<form action="../indent/addIndent.do" name="frmPage" method="post">
<input type="checkbox" name="fruit" value="苹果"/>
<input type="checkbox" name="fruit" value="香蕉"/>
<input type="checkbox" name="fruit" value="橘子"/>
<input type="checkbox" name="fruit" value="荔枝"/>
<input type="checkbox" name="fruit" value="哈密瓜"/>
<input type="button" name="submit1" value="提 交" onclick="submit('submit1');"/>
</form>
<form action=" ……" name="resultFrom" method="post">
<input name="fruit" id="result" type="text" style="display:none"/>
</form>
<script type="text/javascript">
function submit(name){
//清空form的内容
$("form[name=resultFrom]").empty();
//获得所有checkbox
var str = document.getElementsByName(name);
//循环每条记录
var answre = "";
for (var i = 0; i < str.length; i++) {
if (str[i].checked == true) {
answre += str[i].value;
}
}
$("#result").val(answre);
$('#answerForm').submit();
}
</script>
</body>
参考:http://www.cnblogs.com/lcgw/archive/2012/12/10/2810711.html
###· 比对字符串是否含有相同字母 ###
问题描述: 比较两个字符串中是否含有相同的字符。
解决方法:
/**
* 比对两个字符串是否含有相同字符
* @param $trueAnswer 正确的值,或基础值
* @param $myAnswer 要比对的值
* @return bool 正确返回true,错误返回false
*/
public static function checkString($trueAnswer, $myAnswer)
{
$str = str_split($trueAnswer);
foreach ($str as $item){
//strpos 大小写敏感 stripos大小写不敏感
if(stripos($myAnswer,$item) === false){
return false;
}
}
return true;
}