2016-05-20 9 views
0

Ich bin neu in JavaScript und kann nicht herausfinden, warum es nicht funktioniert.KG zu lbs Konvertierung funktioniert nicht richtig

HTML:

<input type="text" id="weight_value" placeholder="Input Weight" /> 
       <select id="weight_unit"> 
        <option value="lbs">KG to Pound</option> 
        <option value="kg">Pound to KG</option> 
       </select> 
       <button title="Convert" onclick="display_result()">Convert</button> 
      <div id="result"></div> 
<script src="hero.js"></script> 

JavaScript:

function display_result(){ 

         var kg = 0.45359237 
         var results = "" 

          if ($(this).val() == "kg") { 
           $("#result").val($("#weight_value").val()*kg) 
          } else { 
           $("#result").val($("#weight_value").val()/kg) 
          } 
         }; 
+1

'val' ist [verwendet für Eingabeelemente] (http://api.jquery.com/val/). '$ ('# result')' ist ein 'div', kein Eingabeelement. Verwenden Sie ['html' stattdessen] (http://api.jquery.com/html/). –

+0

Danke für die Information @AndyTurner. Es funktioniert jetzt. –

Antwort

0

bereits.

function display_result(){ 
    var kg = 0.45359237 
    var results = "" 
    if ($('#weight_unit').val() == "kg") { 
     $("#result").html($("#weight_value").val()*kg) 
    } 
    else { 
     $("#result").html($("#weight_value").val()/kg) 
    } 
}; 
+0

Sie sind ein Lebensretter. Danke vielmals. : D –

+0

Kein Problem. Wenn die Antwort für dich funktioniert hat, kannst du sie als korrekt markieren =) – MCMXCII

+0

hab das schon gemacht :) –