gpt4 book ai didi

javascript - 如何根据 onchange 每个选择框更改每个文本框的值

转载 作者:行者123 更新时间:2023-11-30 09:39:49 25 4
gpt4 key购买 nike

有许多名为 record 的 div。在这些 div 中,我有一个选择框可以更改文本框的值和属性。该代码运行良好。但问题是当一个选择框改变时,所有的文本框值都会改变。如何根据每个选择框更改文本框的值。不是所有的人?这是我的代码:

 

$(function () {
$('select[name=type]').change(function () {
var $option = $(this).find(":selected");
$('input[name=hostInput]').attr('pattern', $option.attr('data-pattern'));
$('input[name=hostInput]').attr('placeholder', $option.attr('data-placeholder'));
$('input[name=hostInput]').attr('title', $option.attr('data-title'));
});
});
  .to_validate:invalid {
color: navy;
outline: none;
border-color: #ff1050;
box-shadow: 0 0 10px #ff0000;
}
.record {
width:50%;
border:1px solid blue;
}
  

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<div class="record">
<select name="type" class="minimal">
<option value="Record" selected hidden>Record</option>
<option value="NS" data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
<option value="MX" data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX </option>
</select>
<br/>
<input type="text" name="hostInput" placeholder="Hostname" class="to_validate" pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" title="Wrong host"/>
</div>
<br/>
<div class="record">
<select name="type" class="minimal">
<option value="Record" selected hidden>Record</option>
<option value="NS" data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
<option value="MX" data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX </option>
</select>
<br/>
<input type="text" name="hostInput" placeholder="Hostname" class="to_validate" pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" title="Wrong host"/>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>

最佳答案

$(function() {
$('select[name=type]').change(function() {
var $option = $(this).find(":selected");
$(this).siblings('input[name=hostInput]').attr('pattern', $option.attr('data-pattern'));
$(this).siblings('input[name=hostInput]').attr('placeholder', $option.attr('data-placeholder'));
$(this).siblings('input[name=hostInput]').attr('title', $option.attr('data-title'));
});
});
.to_validate:invalid {
color: navy;
outline: none;
border-color: #ff1050;
box-shadow: 0 0 10px #ff0000;
}
.record {
width: 50%;
border: 1px solid blue;
}
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
<form>
<div class="record">
<select name="type" class="minimal">
<option value="Record" selected hidden>Record</option>
<option value="NS" data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
<option value="MX" data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX</option>
</select>

<br/>

<input type="text" name="hostInput" placeholder="Hostname" class="to_validate" pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" title="Wrong host" />

</div>
<br/>
<div class="record">
<select name="type" class="minimal">
<option value="Record" selected hidden>Record</option>
<option value="NS" data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
<option value="MX" data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX</option>
</select>

<br/>

<input type="text" name="hostInput" placeholder="Hostname" class="to_validate" pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" title="Wrong host" />

</div>


<button type="submit">Submit</button>
</form>
</body>

</html>

this 上下文与 .siblings() 结合使用

Description: Get the siblings of each element in the set of matched elements, optionally filtered by a selector.

  1. 使用 this 上下文将为您提供当前选择。
  2. 使用 siblings() 会得到当前选择的 sibling ,然后给参数 input[name=hostInput] 会得到准确的输入

关于javascript - 如何根据 onchange 每个选择框更改每个文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com