0

我有如下所示的单选按钮。

        <div id="lensType">

                        <input type="radio"  name="design" style="vertical-align: middle"  value="1"/>
                        <label for="design">Single Vision</label><br/>

                        <input type="radio" name="design" style="vertical-align: middle" value="2" />
                        <label for="material" >Accommodative Support</label><br/>

                        <input type="radio"  name="design" style="vertical-align: middle"  value="3"/>
                        <label for="design">Bifocal</label> <br/>

                        <input type="radio"   name="design" style="vertical-align: middle" value="4" />
                        <label for="material" >Varifocal (Intermediate/Near)</label><br/>

                        <input type="radio"   name="design" style="vertical-align: middle" value="5"/>
                        <label for="material" >Varifocal (Distance/Near)</label>

                    </div>

我正在做一个动态选择。我有发布值的 javascript 代码。似乎现在已发布供应商值。下面是我的脚本的代码。

  $(document).ready(function(){

     function populate() {
      fetch.doPost('getSupplier.php');
   }

 $('#lensType').change(populate);

  var fetch = function() {

 var counties = $('#county');

return {
doPost: function(src) {

$('#loading_county_drop_down').show(); // Show the Loading...
$('#county_drop_down').hide(); // Hide the drop down
$('#no_county_drop_down').hide(); // Hide the "no counties" message (if it's the case)


    if (src) $.post(src, { supplier: $('#lensType').val() }, this.getSupplier);

    else throw new Error('No source was passed !');
},

getSupplier: function(results) {
    if (!results) return;
    counties.html(results);

$('#loading_county_drop_down').hide(); // Hide the Loading...
$('#county_drop_down').show(); // Show the drop down
}   
  }
 }();

 populate();

 });

php代码:

<?php
  if(isSet($_POST['supplier'])) {

   include 'db.php';

  $stmt = $mysql->prepare("SELECT DISTINCT SupplierBrand FROM plastic WHERE          HeadingNo='".$_POST['supplier']."'");
  $stmt->execute();
  $stmt->bind_result($supplierBrand);

  while ($row = $stmt->fetch()) : ?>

 <option value="<?php echo $supplierBrand; ?>" width="100px"><?php echo $supplierBrand; ?></option>

我的问题是,当我调试时,我注意到没有传递给 php 脚本的值,这使得选择为空。我试图通过让firebug输出console.log来跟踪或调试,但在这方面失败了。请协助处理此代码,该代码旨在显示单选按钮选择的动态列表。

4

2 回答 2

2

用于调试:

$('input[name="design"]').change(function(){ 
console.log($('#lensType').find("input:radio[name ='design']:checked").val());
});

别的:

$('#lensType').find("input:radio[name ='design']:checked").val();

代替

$('#lensType').val()

你可能想用一个改变的函数来包装它,因为 onload 没有选择设计:

  $(document).ready(function(){
$('input[name="design"]').change(function(){ 
var design = $('input[name="design"]:checked').val();
     function populate() {
      fetch.doPost('getSupplier.php');
   }

 $('#lensType').change(populate);

  var fetch = function() {

 var counties = $('#county');

return {
doPost: function(src) {

$('#loading_county_drop_down').show(); // Show the Loading...
$('#county_drop_down').hide(); // Hide the drop down
$('#no_county_drop_down').hide(); // Hide the "no counties" message (if it's the case)


    if (src) $.post(src, { supplier: design }, this.getSupplier);

    else throw new Error('No source was passed !');
},

getSupplier: function(results) {
    if (!results) return;
    counties.html(results);

$('#loading_county_drop_down').hide(); // Hide the Loading...
$('#county_drop_down').show(); // Show the drop down
}   
  }
 }();

 populate();
});
 });
于 2013-04-17T10:01:26.603 回答
1

在您的 javascript 中,您得到的是 div 的值,而不是单选按钮:

$('#lensType').val() <--- change that

对于这样的事情:

$("#lensType input:radio[name='design']:checked").val()
于 2013-04-17T09:56:32.363 回答