如何使用javascript激活按钮时,复选框被选中

How to use javascript to activate the button when checkbox is checked?

本文关键字:复选框 何使用 javascript 激活 按钮      更新时间:2023-09-26

我需要这个php文件的修改,但我不知道javascript。这使我很难做这件事。

我想这样做:如果复选框被选中,激活我的提交按钮,否则不要激活它们(默认)。

我已经做了一些修改:我添加了id= checkbox的复选框,并添加了参数disabled="disabled"到我的按钮。

这是工作。现在我只需要在复选框被选中时激活按钮。

但我现在有两个问题:

  1. 我不知道该把javascript代码放到我的文件
  2. 我不知道我可以在我的代码中调用或激活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文件的底部