如何添加必需的属性?
How to add a required attribute?
我有一个显示一些并隐藏一些的脚本 html 但我不知道如何在显示时添加所需的标签,我已经尝试在我想要的部分添加属性但即使没有显示它也需要它......有人可以帮助我吗?提前致谢!
<div class="start"><select name="start" class="form-control">
<option value="0" class="select">Select City</option>
<?php foreach($rows as $row): ?>
<option <? echo 'value="'.$row['id'].'"';
echo ($row['id'] == $job['start'])?' selected>':'>';
echo $row['name']; ?></option>
<?php endforeach; ?>
</select></div><br />
<br />
<script>
$('select[name=start]').change(function () {
if ($(this).val() == '89') {
$('#otherStart').show();
} else {
$('#otherStart').hide();
}
});
</script>
<br />
<div id="otherStart">
<input type="text" name="otherStart" placeholder="Other City" <? echo ($job['otherStart'])?' value="'.$job['otherStart'].'"':'';
?> class="form-control" >
</div>
首先,您需要为正在修改的输入分配一个 id
属性,以便我们可以使用 jQuery 更改属性。您已经使用 <div id="otherStart">
到 show/hide 整个 div,所以 select 另一个名称,例如 id="inputOtherStart"
(ID 应该始终是唯一的)。
然后我们在显示和隐藏div
时分别使用jQuery添加和删除属性。
<script>
$(function() {
// Function to show/hide the extra subject-field, and making it required or not
var Start = '#Start';
var otherStart = '#otherStart';
var otherStartInput = '#otherStartInput';
$(otherStart).hide();
$(Start).change(function(){
if($(Start).val() == 89) {
$(otherStart).show();
$(otherStartInput).prop('required',true);
} else {
$(otherStart).hide();
$(otherStartInput).prop('required',false);
}
});
});
</script>
<br />
<div id="otherStart">
<input type="text" id="otherStartInput" name="otherStart" placeholder="Other City" <? echo ($job['otherStart'])?' value="'.$job['otherStart'].'"':''; ?> class="form-control" />
</div>
要在 div 显示和隐藏时添加和删除属性 "required",您可以将 JavaScript 修改为-
<script>
$('select[name=start]').change(function () {
if ($(this).val() == '89') {
$('#otherStart').show();
$('#otherStart : input').attr('required');
} else {
$('#otherStart').hide();
$('#otherStart : input').removeAttr('required');
}
});
</script>
为了使某个元素在 jQuery 中需要,您应该使用 .prop
:
$(selector).prop("required", true);
您也可以在 JavaScript 中使用属性 required
:
element.required = true;
或者,
element.setAttribute("required","");
我有一个显示一些并隐藏一些的脚本 html 但我不知道如何在显示时添加所需的标签,我已经尝试在我想要的部分添加属性但即使没有显示它也需要它......有人可以帮助我吗?提前致谢!
<div class="start"><select name="start" class="form-control">
<option value="0" class="select">Select City</option>
<?php foreach($rows as $row): ?>
<option <? echo 'value="'.$row['id'].'"';
echo ($row['id'] == $job['start'])?' selected>':'>';
echo $row['name']; ?></option>
<?php endforeach; ?>
</select></div><br />
<br />
<script>
$('select[name=start]').change(function () {
if ($(this).val() == '89') {
$('#otherStart').show();
} else {
$('#otherStart').hide();
}
});
</script>
<br />
<div id="otherStart">
<input type="text" name="otherStart" placeholder="Other City" <? echo ($job['otherStart'])?' value="'.$job['otherStart'].'"':'';
?> class="form-control" >
</div>
首先,您需要为正在修改的输入分配一个 id
属性,以便我们可以使用 jQuery 更改属性。您已经使用 <div id="otherStart">
到 show/hide 整个 div,所以 select 另一个名称,例如 id="inputOtherStart"
(ID 应该始终是唯一的)。
然后我们在显示和隐藏div
时分别使用jQuery添加和删除属性。
<script>
$(function() {
// Function to show/hide the extra subject-field, and making it required or not
var Start = '#Start';
var otherStart = '#otherStart';
var otherStartInput = '#otherStartInput';
$(otherStart).hide();
$(Start).change(function(){
if($(Start).val() == 89) {
$(otherStart).show();
$(otherStartInput).prop('required',true);
} else {
$(otherStart).hide();
$(otherStartInput).prop('required',false);
}
});
});
</script>
<br />
<div id="otherStart">
<input type="text" id="otherStartInput" name="otherStart" placeholder="Other City" <? echo ($job['otherStart'])?' value="'.$job['otherStart'].'"':''; ?> class="form-control" />
</div>
要在 div 显示和隐藏时添加和删除属性 "required",您可以将 JavaScript 修改为-
<script>
$('select[name=start]').change(function () {
if ($(this).val() == '89') {
$('#otherStart').show();
$('#otherStart : input').attr('required');
} else {
$('#otherStart').hide();
$('#otherStart : input').removeAttr('required');
}
});
</script>
为了使某个元素在 jQuery 中需要,您应该使用 .prop
:
$(selector).prop("required", true);
您也可以在 JavaScript 中使用属性 required
:
element.required = true;
或者,
element.setAttribute("required","");