2014-02-10 79 views
0

我有一個工作表單,但我需要它發送一個僅爲文本的值(不包括數字值)。如果有更簡單的方法可以做到這一點,請你幫忙。表單字段計算

這裏的爵士小提琴 - http://jsfiddle.net/qeSZR/83/

基本上當你點擊企業,它計算的值,並將其更改爲3500,當你點擊私人它,它更改爲1800。我的問題是,當你的郵件這個,我有一個說「客戶類型」的字段,其中包括「1800:Private」或「3500:Corporate」作爲其發送的值。我不希望數字,我寧願只是客戶類型=私人或公司。請幫助:) - 謝謝!

下面是代碼:

HTML

 <form action="mailer.php" data-validate="parsley" method="post"> 
      <div class="driver-list"> 
       <p> 
       <h2>Driver 1:</h2> 
       Name <span class="red">*</span>&nbsp; <input name="cf_driver1" data-required="true" type="text" class="form-text" /> 
       Cellphone <span class="red">*</span>&nbsp; <input name="cf_cell1" data-required="true" type="text" class="form-text" /> 
       Email <span class="red">*</span>&nbsp; <input name="cf_email1" data-required="true" data-type="email" type="text" class="form-text" /></p> 
       <h2>Driver 2:</h2> 
       Name <span class="red">*</span>&nbsp; <input name="cf_driver2" data-required="true" type="text" class="form-text" /> 
       Cellphone <span class="red">*</span>&nbsp; <input name="cf_cell2" data-required="true" type="text" class="form-text" /> 
       Email <span class="red">*</span>&nbsp; <input name="cf_email2" data-required="true" data-type="email" type="text" class="form-text" /></p> 
       <h2>Driver 3:</h2> 
       Name <span class="red">*</span>&nbsp; <input name="cf_driver3" data-required="true" type="text" class="form-text" /> 
       Cellphone <span class="red">*</span>&nbsp; <input name="cf_cell3" data-required="true" type="text" class="form-text" /> 
       Email <span class="red">*</span>&nbsp; <input name="cf_email3" data-required="true" data-type="email" type="text" class="form-text" /></p> 
       <h2>Driver 4:</h2> 
       Name <span class="red">*</span>&nbsp; <input name="cf_driver4" data-required="true" type="text" class="form-text" /> 
       Cellphone <span class="red">*</span>&nbsp; <input name="cf_cell4" data-required="true" type="text" class="form-text" /> 
       Email <span class="red">*</span>&nbsp; <input name="cf_email4" data-required="true" data-type="email" type="text" class="form-text" /></p> 
       </div> 
      <div class="vertical-space"></div><p><h2>Entry Type<span class="red">*</span></h2></p> 
      <select name="cf_client_type" id="cf_client_type" size="1" class="option2" > 
      <option value="1800: Private">Private</option> 
      <option value="3500: Corporate">Corporate</option> 
      </select> 
      <p>Corporate entries qualify for a tax donation certificate, please enquire via email.</p> 

      <!-- HIDDEN FIELD - HONEYPOT ANTI_SPAM --> 
      <input id="website" class="taken" name="cf_website" type="text" /> 
      <!-- END --> 

      <!-- HIDDEN FIELD - CALCULATION --> 
      <input id="cf_calculate" class="taken" value="1" name="cf_blank" type="text" /> 
      <!-- END --> 

      <div class="box-contact"><span class="result">Amount Due: &nbsp;R</span><input type="text" name="cf_amount" readonly="readonly" value="1800" id="result"> 

      <input name="Submit" class="submit" value="Submit" type="submit" /> 

JAVASCRIPT

$(document).ready(function(){ 
$("select").change(function(){ 
     var val1 = +parseInt($("#cf_client_type").val()); 
     var val2 = +parseInt($("#cf_calculate").val()); 
     $("#result").val(val1*val2); 
}); 

});

+0

你可以簡化它+ $(「#cf_client_type」)。val();' – Praveen

回答

1

Here is a fiddle

設置你的選擇與data-value爲您計算並保持你想傳遞下去,像這樣的文字塔實際value ......

<option value="Private" data-value="1800">Private</option> 
<option value="Corporate" data-value="3500">Corporate</option> 

然後你的腳本可以像這...

$(document).ready(function(){ 

    $("select").change(function(){ 

    var val1 = +parseInt($("#cf_client_type").find('option:selected').attr('data-value')); 
    var val2 = +parseInt($("#cf_calculate").val()); 
    $("#result").val(val1*val2); 

    }); 

}); 
+0

如果我刪除了金額,那麼計算該值的代碼將不會工作.. – user2382274

+0

將數據值添加到輸入字段您需要的整數,並將實際表單值保留爲所需的文本,並將數據值作爲您的計算對象。 – VIDesignz

+0

如數據值=「1800」 – VIDesignz

0

在你的代碼替換選項值以下

+0

YAMAN

+0

將選項值替換爲與我提到的完全相同的值碼?? :/ – user2382274