使用 Jquery 更改表单输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9576485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:00:15  来源:igfitidea点击:

Change form input value with Jquery

jqueryhtml

提问by FlyingCat

I am trying to change the input field value dynamically when the user pick the options from my dropdown menu. I have tried this code but have no luck. I was wondering if someone here can help me out! Thanks a lot.

当用户从我的下拉菜单中选择选项时,我试图动态更改输入字段值。我试过这段代码,但没有运气。我想知道这里是否有人可以帮助我!非常感谢。

$(document).ready(function(){
$('select[name="job_number"]').change (function () {

    $('input[name="project"]').val()="Good Fish";

});
});

<form action='project_manager' method='post'>
<input type='text' name='project'>show Good Fish when user picks an option</input>
<select name='job_number'>
<option value='1'>job1</option>
<option value='2'>job2</option>
<option value='3'>job3</option>
</select>

</form>

回答by aletzo

try :

尝试 :

$('input[name="project"]').val("Good Fish");

instead of:

代替:

$('input[name="project"]').val()="Good Fish";

回答by Keith Frey

The code below should work. I modified the <input>in the html to be formatted correctly as well as changed $('input[name="project"]').val()="Good Fish";to $('input[name="project"]').val("Good Fish");

下面的代码应该可以工作。我<input>将 html 中的修改为正确格式并更改$('input[name="project"]').val()="Good Fish";$('input[name="project"]').val("Good Fish");

$(document).ready(function(){
    $('select[name="job_number"]').change(function() {

        $('input[name="project"]').val("Good Fish");

    });
});

<form action='project_manager' method='post'>
    <input type='text' name='project' value='show Good Fish when user picks an option' />
    <select name='job_number'>
        <option value='1'>job1</option>
        <option value='2'>job2</option>
        <option value='3'>job3</option>
    </select>
</form>