如何使用javascript激活按钮时,复选框被选中
How to use javascript to activate the button when checkbox is checked?
我需要这个php文件的修改,但我不知道javascript。这使我很难做这件事。
我想这样做:如果复选框被选中,激活我的提交按钮,否则不要激活它们(默认)。
我已经做了一些修改:我添加了id= checkbox的复选框,并添加了参数disabled="disabled"到我的按钮。
这是工作。现在我只需要在复选框被选中时激活按钮。
但我现在有两个问题:
- 我不知道该把javascript代码放到我的文件
- 我不知道我可以在我的代码中调用或激活javascript
如果有人能帮我就太好了。
<?php
if(isset($_POST['photo-name'])){
$photo_name = $_POST['photo-name'];
}
if(isset($_POST['photo-title'])){
$photo_title = $_POST['photo-title'];
}
if(isset($_POST['photo-description'])){
$photo_description = $_POST['photo-description'];
}
if(empty($photo_name)){
$photo_name = '';
}
if(empty($photo_description)){
$photo_description = '';
}
$sql= $wpdb->get_results("SELECT name,id FROM ".$wpdb->prefix."u_gallery_cat ORDER BY name ASC");
$html .= '<div><label for="photo-name"><strong>'.__('Caption:','user-gallery').'</strong></label></div>';
$html .= '<div class="contest-input"><input type="text" name="photo-name" id="photo-name" value="'.$photo_name.'" /></div>';
$html .= '<div><label for="photo-content"><strong>'.__('Description:','user-gallery').'</strong> <span class="contest-small-font-2">'.__('(Optional)','user-gallery').'</span></label></div>';
$html .= '<div class="contest-input"><textarea class="photo-description-textarea" name="photo-description" id="photo-description">'.$photo_description.'</textarea></div>';
if(!empty($sql)){
$html .= '<div><label for="photo-category"><strong>'.__('Category:','user-gallery').'</strong></label></div>';
$html .= '<select name="photo-category" id="photo-category">';
foreach($sql as $item){
$html .= '<option value="'.$item->id.'">'.$item->name.'</option>';
}
$html .= '</select>';
}
if ($photo_limit == 100000000){
$html .= '<div><label for="user-photo"><strong>'.__('Select image:','user-gallery').'</strong></label></div>';
}else{
$html .= '<div><label for="user-photo"><strong>'.__('Select image: (max.','user-gallery').' '.$p_limit.')</strong></label></div>';
}
$html .= '<div class="contest-input"><input type="file" name="user-photo" id="user-photo" size="50" accept="image/*" onchange="loadFileU(event)" class="selimg"></div>';
$html .= '<img id="coutput"/>';
$html .= '<div class="ug-clear"></div>';
$html .= '<input type="hidden" name="user_id" />';
$html .= '<input type="hidden" name="action" value="new_post" />';
$html .= '<div class="contest-button-div">';
$html .= '<div class="contest-button"><input type="checkbox" name="chk" id="chk" value="yourvalue" class="checkbox"></div>';
$html .= '<div class="contest-button"><input type="submit" value="'.__('Add Photo','user-gallery').'" id="submit" disabled="disabled" name="submit" class="ug-styled-button tooglebutton" /></div>';
$html .= '<div class="ug-clear"></div>';
$html .= '</div>';
$html .= '<div class="ug-clear"></div>';
$html .= '</form>';
$html .= '<div class="ug-clear"></div>';
$html .= '</div>';
}
?>
不使用Jquery:
在复选框上添加一个onchange()
事件来触发Javascript:
<div class="contest-button"><input type="checkbox" name="chk" id="chk" value="yourvalue" class="checkbox" onchange="openSubmit()"></div>
脚本:
<script type="text/javascript">
function openSubmit(){ // This function is called by the checkbox click
if(document.getElementById('chk').checked == true){ // If it is checked
document.getElementById('submit').disabled = false; // Then we remove the disable attribute
}
</script>
使用Jquery:
$('#chk').change(function(){ // You put an event on your checkbox here
if($(this).is(':checked')){ // If statut of checkbox is checked
document.getElementById('submit').disabled = false; // Then we remove the disable attribute
}
});
你可以把这个脚本放在你的PHP文件的底部
相关文章:
- 在服务器上创建一个复选框,使用 javascript 处理函数
- 选中复选框是否使用jquery选中
- 复选框说明使用javascript的检索问题
- PHP-AJAX复选框过滤器使用数据标记属性
- 如何在选中数组中的另一个复选框时使用 jquery 取消选中复选框
- 如何选中复选框是否使用javascript
- 在同一复选框上使用 2 个不同的脚本
- 如何在 JavaScript 中选中复选框?(不使用jQuery)
- 我如何通过 id 找到复选框并使用 JQuery 在字符串上应用属性
- 如何打开 html 上的 jQuery 对话框 复选框选中(使用 jQuery)
- 设置 HTML 复选框以使用 javascript 进行检查
- 如何在离子复选框中使用ngModel
- 文本的单事件函数,复选框,使用 Jquery 选择输入
- 选中复选框并使用jQuery启用相应的单选按钮
- 如何清除复选框当选中其他复选框时?使用java脚本
- 显示'复选框仅在选中特定复选框时使用
- 我如何创建一个下拉菜单与标签的复选框列表使用bootstrap
- 如何框架JSON数组从传递复选框值使用php
- 在jade中返回一个复选框值.使用node.js,express和Mongoose
- 如果一个复选框是使用jquery选中的,如何无法选中其他复选框