从下拉框更新文本框

时间:2016-04-06 21:19:49

标签: javascript html

我正在尝试根据我在下拉菜单中选择的内容计算总价;产品价格可以保持不变。

由于某些原因,代码对我不起作用,我做错了什么?

<title>Untitled Document</title>

<script type="text/jscript">

$('#quantity').change(function(){
  var qty = $('#quantity').val();
  var price = $('#productPrice').val();
  var total = price * qty;
  $("#totalprice").val(total);
});

</script>
</head>

<body>

<div class="pricesection">
    <input type="hidden" id="productPrice" value="340"/>
Quantity: 
<select id="quantity">
    <option value="1" selected>1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
</select>
Total: $
<input type="text" id="totalprice" value="340"/>


</div>
</body>

2 个答案:

答案 0 :(得分:0)

select下拉列表中的值和使用javascript检索的输入字段是字符串。使用parseInt()函数将它们转换为整数:

var qty = parseInt($('#quantity').val());
var price = parseInt($('#productPrice').val());

var total = parseInt(price) * parseInt(qty);

另外,请将脚本类型更改为text/javascript,而不是text/jscript

答案 1 :(得分:0)

您需要在html中引用jQuery库。

尝试以下

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x181b9db90 objc_msgSend + 16
1  CoreFoundation                 0x1825314a4 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1548
2  CoreFoundation                 0x182406788 _CFXNotificationPost + 368
3  Foundation                     0x182e0e89c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
4  UIKit                          0x18791f124 __47-[UIApplication _applicationDidEnterBackground]_block_invoke + 320
5  UIKit                          0x1879ef5e4 +[UIViewController _performWithoutDeferringTransitions:] + 128
6  UIKit                          0x18791ef90 -[UIApplication _applicationDidEnterBackground] + 100
7  UIKit                          0x18791f514 -[UIApplication _handleApplicationDeactivationWithScene:shouldForceExit:transitionContext:completion:] + 700
8  UIKit                          0x18792f990 -[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] + 448
9  UIKit                          0x18791961c __70-[UIApplication scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 152
10 UIKit                          0x1879192a4 -[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] + 712
11 FrontBoardServices             0x183ec77ac __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
12 FrontBoardServices             0x183ec7618 -[FBSSerialQueue _performNext] + 168
13 FrontBoardServices             0x183ec79c8 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
14 CoreFoundation                 0x1824dd124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
15 CoreFoundation                 0x1824dcbb8 __CFRunLoopDoSources0 + 540
16 CoreFoundation                 0x1824da8b8 __CFRunLoopRun + 724
17 CoreFoundation                 0x182404d10 CFRunLoopRunSpecific + 384
18 GraphicsServices               0x183cec088 GSEventRunModal + 180
19 UIKit                          0x1876d9f70 UIApplicationMain + 204
20 Trenìt!                       0x1000f32e4 main (AppDelegate.swift:14)
21 libdispatch.dylib              0x181fa28b8 (Missing)

其余的看起来向我提出。

谢谢, 第

修改

这是一个片段

&#13;
&#13;
    <title>Untitled Document</title>

            <script type="text/jscript">

            $('#quantity').change(function(){
              var qty = $('#quantity').val();
              var price = $('#productPrice').val();
              var total = price * qty;
              $("#totalprice").val(total);
            });

            </script>
            </head>

            <body>




            <div class="pricesection">
                <input type="hidden" id="productPrice" value="340"/>
            Quantity: 
            <select id="quantity">
                <option value="1" selected>1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
            </select>
            Total: $
            <input type="text" id="totalprice" value="340"/>


            </div>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
            </body>
&#13;
$('#quantity').change(function() {
  var qty = $('#quantity').val();
  var price = $('#productPrice').val();
  var total = price * qty;
  $("#totalprice").val(total);
});
&#13;
&#13;
&#13;