Jquery自定义复选框和无线电未正确传递值.val()

时间:2012-09-01 18:42:05

标签: jquery ajax plugins

我正在使用此处的插件:http://www.maratz.com/blog/archives/2006/06/11/fancy-checkboxes-and-radio-buttons/

设置单选按钮和复选框的样式。当我在不使用jQuery.get的情况下提交表单时,所有值都传递得很好。但是当我尝试使用以下方法提取值时:

   var sort = $('input[name=sort]:checked').val();

它不起作用。它一直在说" new"无论是" new"收音机或"到期"收音机被检查。我试图弄清楚我到底做错了什么。我还尝试在选择单选按钮时提取用类.r_on指定的标签值,但我也无法设法这样做。

$(document).ready(function(){

var sort = $('input[name=sort]:checked').val();
var location = $('#location').val();


$('.filter_submit').click(function(){
    $.get("../bin/open_tasks.php", "sort=" + sort + "&location=" + location , function(data){
        alert("Data Loaded: " + data);
        });//end get request
    return false;
    });//end click 

});

<fieldset class="radios">
            <label class="label_radio" for="new">Newest<input id="new" checked="checked" class="" type="radio" name="sort" value="new"></label><br>
            <label class="label_radio" for="expire">Expiring First<input id="expire" class="" type="radio" name="sort" value="expire"></label>
        </fieldset>

这是我写的php只是为了测试它:

<?php 

    echo "loaded kinda";

    echo " ".$_GET['sort']." ";
    echo $_GET['location'];

?>

2 个答案:

答案 0 :(得分:0)

你可以试试这个

        if ($('input[name=sort]').is(':checked')) {

而不是

     var sort = $('input[name=sort]:checked').val();

编辑:

这个怎么样

        $('input[name=sort]').change(function () {
             //this will fire when radio button is clicked
             //store this information somewhere
           }

答案 1 :(得分:0)

您正在获取页面加载值(.ready)。这太早了 - 它将是当时的价值,它始终是相同的。

您可能希望在提交时获取值,以便更新单选按钮可以获得更新的值。只需将.val()行移到.click处理程序中即可。 #location行也是如此。