我正在尝试从其他字段更改从javascript(jQuery)设置的值。我想只从该值中定位一个单词。这是我的代码(http://jsfiddle.net/zL4pc90d/):
<html>
<head>
<title>jQuery get input Text value example</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"
type="text/javascript"></script>
<script
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#select_car").change(function()
{
if ($("#select_car").val() == "")
{
terms = "";
} else if ($("#select_car").val() == "Honda")
{
terms = "One year lease on <car>.";
} else if ($("#select_car").val() == "Toyota")
{
terms = "Two year lease on <car>.";
} else if ($("#select_car").val() == "Ford")
{
terms = "Three year lease on <car>.";
}
$("#contract").val(terms);
});
});
</script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<form>
<fieldset>
<legend>jQuery Get and Set input Text value</legend>
<p>
<label for="carModel">
Car Model:
</label>
<input id="myInputText" type="text" name="inputBox" />
</p>
<p>
Make:
<select id="select_car">
<option value></option>
<option value="Honda">Honda</option>
<option value="Toyota">Toyota</option>
<option value="Ford">Ford</option>
</select>
</p>
<p>
<label for="contract">
Contract:
</label>
<input id="contract" type="text" name="outputBox" readonly="readonly" />
</p>
</fieldset>
</form>
</div>
</body>
</html>
对于新值,无论它在哪里说“”,我都希望它是我在第一个输入字段中输入的内容。我也喜欢动态地做,所以也许可以调用keyup函数。我该怎么做?
答案 0 :(得分:0)
您可以使用input
事件跟踪用户在输入字段中输入的内容,并相应地更新另一个框,
$("#myInputText").on("input", function () {
if ($("#myInputText").val() != "") {
terms = "Three year lease on " + $("#myInputText").val();
} else terms = terms = "Three year lease on <car>";
$("#contract").val(terms);
});