2015-03-19 36 views
0

嘿我想檢查是否複選框被選中,然後如果選中將該數據拉到我的窗體中的隱藏字段作爲我的表單發佈的值。這是我正在與之合作。從引導和jQuery的複選框拉值

### Update User Profile 
if (@$_POST['savePrivacy']) { 

// error checking 

// update user 
if (!$errorsAndAlerts) { 
    $colsToValues = array(); 
    $colsToValues['show_contact']   = $_REQUEST['show_contact']; 
    $colsToValues['test']   = $_REQUEST['test']; 
    // ... add more form fields here by copying the above line! 
    $colsToValues['updatedByUserNum'] = $CURRENT_USER['num']; 
    $colsToValues['updatedDate=']  = 'NOW()'; 
    mysql_update(accountsTable(), $CURRENT_USER['num'], null, $colsToValues); 

    // on success 
    $errorsAndAlerts = "Thanks, we've updated your privacy settings!<br/>\n"; 

    redirectBrowserToURL('edit-profile.php'); 

} 
} 

<form method="post" role="form" action=""> 
<input type="hidden"name="savePrivacy" value="1" /> 


<div class="checkbox"> 
<span class="checked"> 
<label> 
<input type="checkbox" id="view"> Yes</label> 
<input type="hidden" name="show_contact" value="0"> 
<script> 
var state = window.jQuery("#view").prop("checked") ? 1 : 0; 
$('input[name="show_contact"]').val(state); 
</script> 
</span> 
</div> 

<input class="button btn green-haze" type="submit" name="submit" value="Save" /> 

</form> 

我想將檢查狀態的值拉到隱藏值。幫助將非常感謝!

回答

0

請試試這個,

var state = window.jQuery("#view").is(":checked") ? 1 : 0; 
$('input[name="show_contact"]').val(state);