如何用jquery&传递单选按钮值PHP?

时间:2015-06-26 07:51:45

标签: javascript php jquery

我正在使用 javascript php 代码传递 输入的值 keyup 上的 字段。 这工作正常。但我想添加一些单选按钮,无法传递它们的价值。我正在使用的代码是:

HTML

<div class="header-right">
<input style="width:150px;" id="searchData" type="text"><br> 
<input type="radio" id="advsrch" name="advsrch" value="name">Name
<input type="radio" id="advsrch" name="advsrch" value="city">City
</div>

的Javascript

$(document).ready(function(){

    var advsrch = "";
var selected = $("input[type='radio'][name='advsrch']:checked");
    advsrch = selected.val();


$('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');

  $('#searchData').keyup(function() {

 var searchVal = $(this).val();
      if(searchVal !== '') {

         $.get('search_data/search-data.php?searchData='+searchVal+advsrch, function(returnData) {
             if (!returnData) {
                $('#results').html('<p style="padding:5px;">Search term entered does not return any data.</p>');
            } else {
                $('#results').html(returnData);
            }
        });
    } else {
        $('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');
    }

    });

});

然后在 PHP

中获得价值
$param = $_GET["searchData"];
$advsrch = $_GET["advsrch"];

此处我正在获得 searchData 的价值,但 advsrch

4 个答案:

答案 0 :(得分:1)

更新,(如下所示),测试并100%工作(提交截图)


    <script src="https://code.jquery.com/jquery-2.1.4.js"></script>

    <div class="header-right">
        <input style="width:150px;" id="searchData" type="text"><br>
        <input type="radio" id="advsrch" name="advsrch" value="name">Name
    </div>



    <script>
        $(document).ready(function(){

            var advsrch = "";
            var selected = $("input[type='radio'][name='advsrch']:checked");
            advsrch = selected.val();


            $('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');

            $('#searchData').keyup(function() {

                var searchVal = $(this).val();
                if(searchVal !== '') {

                    $.get('search_data/search-data.php',{ searchData:searchVal, advsrch:advsrch }, function(returnData) {
                        if (!returnData) {
                            $('#results').html('<p style="padding:5px;">Search term entered does not return any data.</p>');
                        } else {
                            $('#results').html(returnData);
                        }
                    });
                } else {
                    $('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');
                }

            });

        });
    </script>

enter image description here

答案 1 :(得分:1)

' advsrch '的值似乎总是未定义

将以下代码放在advsrch = selected.val();之后:

if( $('#advsrch').attr("checked") != 'checked'){
    advsrch=0;
}else{
    advsrch=1;
}

如果未选中复选框,则会将 advsrch 设置为0,否则该值将为1.

答案 2 :(得分:0)

你的网址没有传递advsrch参数。你的网址是

$.get("search_data/search-data.php?searchData="+searchVal+"&advsrch="+advsrc, function(returnData) {

答案 3 :(得分:0)

现在我得到两个值脚本,如

<强> HTML

<div class="header-right">
<input style="width:150px;" id="searchData" type="text"><br> 
<input type="radio" id="advsrch" name="advsrch" value="name">Name
<input type="radio" id="advsrch" name="advsrch" value="city">City
</div>

<强>的Javascript

$(document).ready(function(){

$('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');

  $('#searchData').keyup(function() {

  var advsrch = "";
var selected = $("input[type='radio'][name='advsrch']:checked");
  advsrch = selected.val();

 var searchVal = $(this).val();
      if(searchVal !== '') {

         $.get("search_data/search-data.php?searchData="+searchVal+"&advsrch="+advsrch, function(returnData) {
             if (!returnData) {
                $('#results').html('<p style="padding:5px;">Search term entered does not return any data.</p>');
            } else {
                $('#results').html(returnData);
            }
        });
    } else {
        $('#results').html('<p style="padding:5px;">Enter a search term to start filtering.</p>');
    }

    });

});