【问题标题】:Getting the value of select option not working获取选择选项的值不起作用
【发布时间】:2016-01-11 08:41:17
【问题描述】:

我有一个带有 2 个选项(本地和进口)的下拉列表。如果我选择 LOCAL,我应该得到值 LOC,而 IMPORT,我应该得到 IM。但无论是我选择的两个,我都得到了价值 IM。请帮忙,如何获取对应select选项的值。谢谢。

<?php  $shipmenttype = array("0"=>"Please select", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype' onchange=''>
    <?php foreach($shipmenttype as $keyname=>$ship_type):?>

        <?php 
            $selected = "";
                    $shiptype = $_POST['shiptype'];
                    if($keyname == $shiptype): $selected =" selected"; endif;       
        ?>                    
                <option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
            <?php endforeach;?>

</select></b></td></tr>


<?php

$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($keyname);

if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}

$refnumb = $keyname.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT); 

?>

运行编号(000000):

function dbNextID($key) {
$sql1 = "insert into key_master (keyname) values (:keyname)";
$sql2= "update key_master set id = id + 1 where keyname = :keyname";
$sql3 = "select id from key_master where keyname = :keyname";

$conn = dbConnect(); 
$stmt1 = $conn->prepare($sql1);
$stmt2 = $conn->prepare($sql2);
$stmt3 = $conn->prepare($sql3);

$conn->beginTransaction();
$stmt1->execute(array(':keyname' => $key));
$stmt2->execute(array(':keyname' => $key));
$stmt3->execute(array(':keyname' => $key)); 
$value = $stmt3->fetchColumn(0);
$conn->commit();
$conn=null;

return $value;
}

HTML 表格:

<!--- Local Shipment --->

<table id="t1" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship1' id='ship1' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly /></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier1'>
    <?php foreach($data1 as $row1): ?>
    <option value="<?php echo $row1['SupplierName'] ?>"><?php echo $row1['SupplierName'] ?></option>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description1' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no1' /></td></tr> 

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number1' /></td></tr>

<tr><th align='right'>Receive Qty: </th>
<td><input type='text' name='r_qty1' /></td></tr>

<tr><th align='right'>No. of Boxes/Packs: </th>
<td><input type='text' name='no_boxes1' /></td></tr>

<tr><th align='right'></th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='local_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='local_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>


<!--- Import Shipment --->

<table id="t2" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship2' id='ship2' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><b><input type='text' name='ref_no2' id='ref_no2' value="<?php echo $refnumb ?>" readonly /></b></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier2'>
    <?php foreach($data2 as $row2): ?>
    <option value="<?php echo $row2['SupplierName'] ?>"><?php echo $row2['SupplierName'] ?>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description2' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no2' /></td></tr>

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number2' /></td></tr>

<tr><th align='right'>AWB(Airway Bill) No: </th>
<td><input type='text' name='awb_no2' /></td></tr>

    <tr><th align='right'>AWB No of Boxes/Packs: </th>
<td><input type='text' name='awb_no_boxes2' /></td></tr>

<tr><th align='right'> </th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='import_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='import_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>

jquery:

<script>
$('#shiptype').change(function () {
$('#t1,#t2').hide();
    var selectedValue = $(this).val();
var strloc = "LOCAL";
var strimp = "IMPORT";

    if (selectedValue == "LOC") {
        $('#t1').show();
    $('#ship1').val(strloc);
    //$('#strkey').val(selectedValue);

    } else if (selectedValue == "IM") {
        $('#t2').show();
    $('#ship2').val(strimp);
    //$('#strkey').val(selectedValue);
    }           
 });

$('#t1,#t2').hide();

</script>

【问题讨论】:

  • 如何在一个下拉菜单中选择两个选项?请简要解释一下。
  • 您问题中的代码看起来没问题。请在您评估下拉列表中所选选项的位置添加代码。
  • 你在寻找多种选择吗??
  • @mapek 请在上面查看我更新的代码。

标签: php sql


【解决方案1】:

你可以在你的代码中试试这个:

$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');

您的完整代码(修改后):

<select name='shiptype' id='shiptype' onchange='' >
<?php foreach($shipmenttype as $keyname=>$ship_type):?>

<?php 
$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');
?>                    
<option value="<?php echo $keyname;?>" <?php echo $selected;?>>

<?php echo $ship_type;?>
</option>
<?php endforeach;?>

</select>

现有代码的解决方案:

$selected = "";
if($keyname == $shiptype): $selected ='selected=""'; 
endif;       

我改变了什么?

只需替换:

$selected ="selected";

与:

$selected ='selected=""';

【讨论】:

  • 仍在获取本地的 IM
  • 在 if else 条件之前使用 var_dump($_POST);。 @KimlynTormes
  • 具体在哪里?我试图把它放在 $shiptype = $_POST['shiptype']; $selected = ($shiptype == $keyname ? 'selected=""' : '');但什么也没发生
  • 没有。我正在使用 jquery 只显示表格。我没有提交按钮
猜你喜欢
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 2021-02-26
  • 1970-01-01
相关资源
最近更新 更多