简体   繁体   English

jQuery根据下拉列表选项更改文本

[英]jQuery change text based on drop down list option

Hi I am looking for guidance on how I can change the interest rate value based on what type of account a customer chooses from the drop down. 您好,我正在寻找有关如何根据客户从下拉菜单中选择哪种帐户来更改利率值的指南。

Form: 形成:

<form id="account" action="" method="post">
<select id = "acc_type">
<option value="current">Current</option>
<option value="savings">Savings</option>
</select>

<input type="text" id="interest" value="">

</form>

<script>
$("#account").change(function() {
    var rate = "0.6%";
    if $(#acc_type).val == ("current") {
        rate = "0.6%");
    } else if $(#acc_type).val == ("savings") {
        rate = "0.8%");
    }
    $(#interest).val = rate;
</script>

http://jsfiddle.net/rmjKV/ Can I have some explanation of why this does not work? http://jsfiddle.net/rmjKV/我可以对为什么这不起作用有一些解释吗?

It does not work since that is not valid JavaScript. 它无效,因为那是无效的JavaScript。 Use a tool like JSLInt to check your code to see the errors. 使用JSLInt之类的工具检查您的代码以查看错误。

You are missing many ( and ) in that code. 您在该代码中缺少许多()

Look at the if statements on MDN 查看MDN上的if语句

if (cipher_char == from_char) {
   result = result + to_char;
   x++;
} else {
   result = result + clear_char;
}

Do you see what you are missing? 你看到你所缺少的吗? The ( and ) around the check. 支票周围的(和)。

Now jQuery val is a method, you are not calling a method. 现在jQuery val是一个方法,您没有在调用方法。 You need (). 你需要 ()。 You can not set .val either. 您也不能设置.val。

The docs for jQuery val() jQuery val()文档

Your selector is also wrong, you are missing quotes. 您的选择器也是错误的,您缺少引号。

Learn to use your console! 学习使用控制台! It will show you the errors. 它会向您显示错误。

You have a lot of syntax errors and improper way of using jQuery functions and selectors 您有很多语法错误,并且使用jQuery函数和选择器的方式不正确

$("#account").change(function() {
    var rate = "0.6%";
    if($('#acc_type').val() == "current") 
    {
        rate = "0.6%";
    }
    else if ($('#acc_type').val() == "savings") 
    {
        rate = "0.8%";
    }
    $('#interest').val(rate);
});  

You need to read more about JS syntax and jQuery selectors and functions use 您需要阅读有关JS语法和jQuery选择器以及函数使用的更多信息

Check the fiddle update http://jsfiddle.net/sedz/rmjKV/2/ 检查小提琴更新http://jsfiddle.net/sedz/rmjKV/2/

JSHint doesn't validate your code. JSHint不会验证您的代码。 Aside that, it looks like you are binding to the form change not the acc_type change. 除此之外,看起来您正在绑定到form更改而不是acc_type更改。

$("#acc_type").change(function() {

}

Valid JS Code: 有效的JS代码:

$("#acc_type").change(function()
{
   var rate = "0.6%";
   if ($("#acc_type").val() == "current")
   {
      rate = "0.6%";
   } else if ($("#acc_type").val() == "savings")
   {
      rate = "0.8%";
   }

   $("#interest").val(rate);
});

http://jsfiddle.net/rmjKV/5/ http://jsfiddle.net/rmjKV/5/

Stuff like this 像这样的东西

$(#acc_type)

should be 应该

$('#acc_type')

You've got numerous errors in your code. 您的代码中有许多错误。 I think I've fixed most of them below. 我想我已经在下面修复了大多数问题。

I've also wrapped you code in jquery's ready function, so the the script doesn't try and add the event before the page is fully loaded etc. 我还将代码包装在jquery的ready函数中,因此脚本不会在页面完全加载之前尝试添加事件等。

$(document).ready(function() {
    $('#account').change(function() {
        var rate = "0.6%";
        if ($('#acc_type').val() == "current") {
            rate = "0.6%";
        } else if ($('#acc_type').val() == "savings") {
            rate = "0.8%";
        }
        $('#interest').val(rate);
    })
})
$("#acc_type").change(function() {
    var rate = "0.6%";
    if ($('#acc_type').val() == "current") {rate = "0.6%";}
    else if ($('#acc_type').val() == "savings") {rate = "0.8%";}
    $('#interest').val(rate);
});

Find here http://jsfiddle.net/rmjKV/8/ your jsFiddle working, your old code was full of typos 在这里找到http://jsfiddle.net/rmjKV/8/您的jsFiddle工作正常,您的旧代码充满了错别字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM