简体   繁体   English

如何在没有提交按钮的情况下检查单选按钮是否被选中?

[英]How can i check if the radio button is checked or not without a submit button?

How can i check the value of the radio box whether its Pearson or euclidean. 我如何检查单选框的值是Pearson还是Euclidean。 So what i have now is this: 所以我现在是这样的:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {

        if($_POST['radio'] == 'Euclidean'){
        $get_recommendations = get_recommendations($new_videos,$user_data['username'], $sim_score='sim_euclidean');
        }

    }
    // as default i want the pearson method to be used
    else{
        $get_recommendations = get_recommendations($new_videos,$user_data['username'], $sim_score='sim_pearson');

Do i have to have a button to subit the value chosen, or is there a way to get the value without having a submit button? 我是否需要一个按钮来替换所选的值,或者是否有一种无需提交按钮即可获取值的方法?

This is the form i have: 这是我的表格:

          <form action="" method="post"> 
          <div class="control-group">
            <h1 style="font-size:22px;">Choose method to recommend with:</h1>
            <label class="control control--radio">Pearson
              <input type="radio" name="radio" value="Pearson" checked="checked"/>
              <div class="control__indicator"></div>
            </label>
            <label class="control control--radio">Euclidean
              <input type="radio" name="radio" value="Euclidean"/>
              <div class="control__indicator"></div>
            </label>
          </div>
          </form>

With jquery, give radio a class or id of radio for example: 使用jquery时,请给radio一个radio的类或ID:

$(document).ready(function()){

$('input:radio[name="radio"]').change(function()){
var value = $('form input[type=radio]:checked').val();  // Gets val of checked on change event

// code to manipulate it, can also be sent to a php file via ajax if you need it that way
}

}

Link for passing variables around jquery to php Passing jQuery Value to PHP 链接,将围绕jquery的变量传递给php将jQuery值传递给PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM