在javascript中相当于isset($_POST['name'])
What is the equivalent of isset($_POST['name']) in javascript?
我在两个不同的PHP页面上有两个表单(由复选框组成)。我想使用从第一个表单提交的值来禁用第二个表单中的复选框。
页1.php:
<form method="POST" action="page2.php">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="submit">
</form>
页2.php:
<form method="POST" action="action.php">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="submit">
</form>
<script>
if(!isset($_POST['2'])){
document.getElementById("4").disabled = true;
}
</script>
我应该用什么来代替这个?
if(!isset($_POST['2']))
由于教授的限制,我无法使用jQuery。
首先不要忘记,PHP 的复选框上需要名称属性。其次,虽然你的ID在HTML5中是有效的,但我会将它们更改为以字母开头,以使它们与HTML4兼容。
我使用 php 打印 POST 变量并将其分配给 javascript 中的post
。然后我检查该复选框是否存在于发布变量中。
页1.php:
<form method="POST" action="page2.php">
<input type="checkbox" id="1" name="box1">
<input type="checkbox" id="2" name="box2">
<input type="checkbox" id="3" name="box3">
<input type="checkbox" id="4" name="box4">
<input type="checkbox" id="5" name="box5">
<input type="checkbox" id="6" name="box6">
<input type="submit">
</form>
页2.php:
<form method="POST" action="action.php">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="submit">
</form>
<script>
var post = <?php echo json_encode($_POST) ?>;
if (!post.box2) document.getElementById("4").disabled = true;
</script>
由于 JavaScript 运行在客户端而不是服务器上,因此它是无状态的。因此,您在page2.php
上的JavaScript不知道从page1.php
提交的值。
为了解决这个问题,你需要在 JavaScript 中使用 PHP,像这样page2.php
:
<script>
if (<?php echo !isset($_POST['a']) ? 'true' : 'false'; ?>) {
document.getElementById("4").disabled = true;
}
</script>
你可以
这样做。丑陋但有效:
<script>
if (<?php echo !isset($_POST['2']); ?>) {
document.getElementById("4").disabled = true;
}
</script>
您的复选框必须具有名称:
<form method="POST" action="page2.php">
<input type="checkbox" name="someVar1" id="someVar1" />
<input type="checkbox" name="someVar2" id="someVar2" />
<input type="checkbox" name="someVar3" id="someVar3" />
</form>
然后在第二页的脚本中:
<script>
<?php
if( !isset($_POST['someVar2']) || !$_POST['someVar2'] ){
echo 'document.getElementById("someVar2").disabled = true;';
}
?>
</script>
试试这个:
<?php if (!isset($_POST['2'])) { ?>
<script>
document.getElementById("4").disabled = true;
</script>
<?php } ?>
PHP解决方案:
<input type="checkbox" id="2" name="2" />
<!-- ... -->
<!-- rather than disable if it isn't set, only show if it is set -->
<?php if (isset($_POST['2'])) { ?>
<input type="checkbox" id="4" name="4" />
<?php } ?>
请注意,您的输入必须具有名称。
可以尝试如下操作
var post_val = <?= json_encode($_POST) ?>;
if (typeof post_val['2'] == 'undefined') {
document.getElementById("4").disabled = true;
}
Jquery可以简化:对于input/textarea/,你可以使用 $('#id_for_input_or_textarea').val() != '' 来检查它是否为空。对于单选/复选框,您可以使用 $(':radio:checked').length != 0/$(':checkbox:checked').length != 0 来检查是否选中了一组。或 $('#some_radio').prop('checked') 以检查是否选中了单选/复选框。
相关文章:
- 如何使用WCF服务和javascript表单post上传.doc文件
- Jquery POST未填充数组
- 如何在php文件中获取$.post-ajax传递的值
- 在POST中将html表作为csv提交
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 如何使用post方法从方法上传文件
- Django: AJAX + CSRF POST gives 403
- AJAX不会在文件上传后重定向到网页-POST方法
- AngularJS单选筛选不适用于Name、Description和Field4复选框值
- 下载使用POST数据动态生成的文件
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 如何使用自定义标头跨域执行AJAX POST
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- 正在检测导航到<a name=“;最新主题”></a>
- jQuery在输入下验证post错误消息
- 在文本区域POST后解码JSON
- Node.js - POST to iFrame?
- 如何使用“id”而不是“name”来POST HTML表单
- JQuery post and name attribute
- 如何使用Ajax post接收表单NAME