duanhanzi8328 2013-04-10 04:47
浏览 29
已采纳

带Onchange的JScript变量

After a bit of help and learning, I have managed to change a target via JScript, but I can't seem to figure out how to make a variable or make it do a calculation.

My code so far is (these are three Form Boxes + a Submit Button):

NUMBER
<input name="contact_build" 
       type="text" 
       id="contact_build" 
       value="<?php echo $row_contact['contact_build']; ?>" />



TYPE
<select id="contact_type" 
        name="contact_type" 
        onchange="targ.value=this.value" />
        <?php
        $specresult = mysql_query("SELECT * FROM `types` ORDER BY type_value");
        while ($specrow = mysql_fetch_array($specresult)) {
            echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
        }
        ?>
</select>



VALUE
<input name="contact_value" id="targ" value="<?php
        $var1 = $row_contact['contact_build'];
        $var2 = $row_value['type_value'];
        $total = round($var2 * $var1);
        echo "" . $total . "";
        ?>" />


<input type="submit" name="Submit2" value="Update" />

What I want to do is make "TARG" the "$VAR2" variable, so that it can work out the VALUE calculation then echo (or what ever command is required) the answer in the VALUE box, as soon as the user changes the option in the TYPE box. Hope this makes sense.

At the moment I have managed to get it to show the "TYPE" value in the "VALUE" box. It ignores any "echo" command above.

Am I looking at this incorrectly and it should be done another way?

PS: I know it's MYSQL; I am working on learning PDO as we speak to change it over. :-) But for the moment, I just want to figure this out using the language that I have basic knowledge of.

  • 写回答

2条回答 默认 最新

  • douzhi9939 2013-04-10 05:04
    关注

    Go through the code below.

    <body>
    
        <input name="contact_build" 
          type="text" 
          id="contact_build" 
          value="<?php echo $row_contact['contact_build']; ?>" />
    
        <select onchange="change(this.value)" >
            <option value="1"> 1</option>
            <option value="2"> 2</option>
            <option value="3"> 3</option>
        </select>
    
        <input type="text" id="targ" value="2"/>
    
        <script>
            function change(val) {
                var cnt_bld = document.getElementById("contact_build").value;
                var total_val = Math.round(parseInt(val) * parseInt(cnt_bld));
                document.getElementById("targ").value = total_val;
            }
        </script>
    
    </body>
    

    In the above code i have called a function change(this.value) which will accept the select box value. Then in the function I have used that value to change the value of input field. Look at document.getElementById. With your code your were directly using a id instead. Hope this helps.

    EDIT this line

    echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
    

    can be done like this if you have those name and value.

    echo '<option value="' . $specrow['type_value'] . '">' . $specrow['type_name'] . '</option>';
    

    assuming your another column name as type_value where values are stored like 100,200and so on and type_name is the name like good, better or best.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序