PHP-如果未选择选项,如何禁用超链接?

时间:2017-06-15 06:41:46

标签: javascript php twitter-bootstrap

如果未选择选项,我试图禁用超链接。问题是这是我公司的一个现有项目,原来的编码器已经不在了。所以我找不到HTML源代码。我只有这个阵列。我尝试使用javascript,就像我们在<option>标记中一样,但它不起作用。

array("type"=>"select", "name"=>"agentId", "value"=>getAgentASMOption($partyCode), "title"=>"Select Party" ),

<td class="no-border" align="center">
    <a href="<?php echo SITEURL; ?>/productnew/productnew-details/?id=<?php echo $d['mmID']; ?>&mCatID=<?php echo $_REQUEST["mCatID"];?>  ">
     <label>View</label>
    </a>
</td>



function getAgentASMOption($partyCode=0) {
    global $DB;
    $sel = "";
    $sql ="SELECT DISTINCT(A.partyCode), P.NAME1, P.ORT01 FROM`".$DB->pre."agent_party` AS A LEFT JOIN `".$DB->pre."party_master` AS P ON P.KUNNR=A.partyCode where A.agentCode = '".sprintf('%d',$_COOKIE['PARTYCODE'])."'  ORDER BY P.NAME1 ASC"; 

    $res = $DB->dbRows($sql);
    if($DB->numRows> 0){
        foreach($res as $asm) {
            $sel = "";
            if($asm['partyCode'] == $partyCode) { $sel = "selected"; }
            //$str .= '<option value="'.$asm['partyCode'].'" '.$sel.'>'.$asm['NAME1'].'</option>';
            $str .= '<option value="'.$asm['partyCode'].'" '.$sel.'>'.$asm['NAME1'].' ----- '.$asm['ORT01'].' -----  '.$asm['partyCode'].' </option>';
        }   
    }   
}

呈现HTML: -

<select id="agentId" name="agentId" title="Select"></select>

    <option value="">--Select--</option>
    <option value="1002410">(MUMBAI) SANDEEP FABRICS ----- KALBADEVI -----  1002410 </option>
    <option value="1013283">(PALGHAR)BLUE SKIES FASHION AVENUE ----- PALGHAR -----  1013283 </option>
</select>



  $sql = "SELECT NAME1,ORT01 FROM `" . $DB->pre . "party_master`WHERE `KUNNR`= '" .$_SESSION['party_Code']. "'";
           $res = $DB->dbRow($sql);

1 个答案:

答案 0 :(得分:1)

如果你给链接一个ID或类,那么选择比我现在要做的更安全

注意:链接没有禁用属性,因此下面的代码使用CSS和事件preventDefault

假设select有一个<option value="">Select Party</option>,你可以这样做:

$(function() {
  $("#agentId").on("change", function() {
    if ($(this).val() == "") {
      $(".proddet").prop("disabled","disabled");
      $(".proddet").addClass("disabled");
    } 
    else {
      $(".proddet").removeProp("disabled");
      $(".proddet").removeClass("disabled");
    }
  }).change(); // in case a "selected" option is already present
  $(".proddet").on("click",function(e) {
    if ($(this).prop("disabled") !== undefined) {
      e.preventDefault();
    }
  })
});
a[disabled] { pointer-events:ignore; color:lightgray }
a.disabled { pointer-events:ignore; color:lightgray }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<select id="agentId" name="agentId" title="Select">
<option value="">--Select--</option>
<option value="1002410">(MUMBAI) SANDEEP FABRICS ----- KALBADEVI -----  1002410 </option>
<option value="1013283">(PALGHAR)BLUE SKIES FASHION AVENUE ----- PALGHAR -----  1013283 </option>
</select>

<table>
  <tr>
    <td class="no-border" align="center">
      <a class="proddet"  href="/productnew/productnew-details/?id=<?php echo $d['mmID']; ?>&mCatID=bla">
        <label>View</label>
      </a>
    </td>
  </tr>
</table>