2014-03-03 36 views
-1

我想僅在選中複選框時提交表單,或者顯示「請選中複選框」,有沒有辦法做到這一點?任何人都可以引導我嗎?如何檢查複選框時提交表單?

感謝

<form name="myForm" action="mailsent.php" method="post"> 

<input type="checkbox" class="chk_row" name="chk1[]"" value=" '.$rows["id"].'"/> 

<input type="submit" value="" style="margin:7px 26px -27px 1424px;background-image: url(/image/exporttt.png);background-repeat: no-repeat;cursor:pointer;" > 

</form> 

編輯的代碼:

<form name="myForm" action="mailsent.php" method="post"> 

    <input type="checkbox" class="chk_row" id="chk1" name="chk1[]"" value=" '.$rows["id"].'"/> 

    <input type="submit" value="" style="margin:7px 26px -27px 1424px;background-image: url(/image/exporttt.png);background-repeat: no-repeat;cursor:pointer;" > 

    </form> 

<script> 

$('form').submit(function(){ 
    if(!$('#chk1').is(':checked')){ 
     alert("Please Check "); 
     return false; 
    } 
}); 
</script> 
+2

您應該先嚐試一下。 –

+0

可能重複[提交表單時檢查複選框](http://stackoverflow.com/questions/10602470/submitting-a-form-when-a-checkbox-is-checked) – Rikesh

+1

我認爲你的問題標題是錯誤。我閱讀它時應單擊複選框提交表單。 – RRikesh

回答

1

使用jQuery
你應該給你的複選框唯一ID

$('form').submit(function(){ 
var flag=0; 
    $('.chk_row').each(function(){ 
    if(($(this).is(':checked'))){ 
     flag=1 
     return false; 
    } 
    }); 
    if(flag==0){ 
    alert("Please Check Checkbox"); 
    return false 
    } 
}); 

你your_checkboxId是你給的ID在你輸入chekcbox 例如。

<input type="checkbox" id="your_checkboxId" name="chk_name" value="some"/> 
+0

表格中的這些複選框應該提交時,我檢查複選框,然後單擊提交按鈕,如果我提交按鈕而不選中複選框,我不想提交表單 – arok

+0

通過此代碼,我沒有提交表單請檢查,你必須給你的複選框@arok唯一的ID由

+0

請看我編輯的代碼 – arok

0

您可以驗證使用jQuery,給你的ID爲 '提交' 提交按鈕,然後使用此代碼

$('#submit').click(function(e){ 
    e.preventDefault(); 
    if($("[name='chk1[]']").is(':checked')){ 
     return true; 
    } else { 
    // your custom code here 
     return false; 
    } 
}) 
0

試試這個

$("#submit").click(function(e) 
    { 
    if($("#check").is(':checked')) 
    { 
     $("#form").prop("action","mailsent.php"); 
    }else 
    { 
    e.preventDefault(); 
    } 
    }); 

而且你不需要指定的在窗體標籤中的行爲。 #form是表單的ID,#check是複選框的ID。

0

假設這裏只有一個複選框,並且我使用類名選擇它。您可以根據實施情況修改選擇器以選擇所需的選擇器。

$(document).ready(function(){ 
$('.chk_row').change(function(e){ 
if($(this).attr('checked') === 'checked') 
{ 
$('form[name="myForm"]').submit(); 
} 
}); 
});