我的代码如下所示:
form action="familytree.php" method="post">
<?php
foreach ($spouses as $spouse) {
if (!empty($spouse['mname'])) {
$name = $spouse['fname'].' '.$spouse['lname'].' ('.$spouse['mname'].')';
}
else {
$name = $spouse['fname'].' '.$spouse['lname'];
}
if ($spouse['ended'] == '1') {
$married = '';
$divorced = 'checked';
}
else {
$married = 'checked';
$divorced = '';
}
?>
<div class="form_section dates">
<h3><?php echo $name; ?></h3>
<p>
<input type="radio" id="married_option" name="married_divorced_options" <?php echo $married; ?> value="1"/>
<label for="edate">Married</label>
<input type="radio" id="divorced_option" name="married_divorced_options" <?php echo $divorced; ?> value="1"/>
<label for="sdate">Divorced</label>
</p>
<div class="half" style="display: inline">
<input type="text" name="sdate_<?php echo $spouse['individual_id']?>" id="sdate_<?php echo $spouse['individual_id']?>" value="<?php echo $spouse['start_date']; ?>" placeholder="Rašykite sutuoktuvių datą čia"/>
<?php echo $this->formError->error("sdate_".$spouse['individual_id'].""); ?>
</div>
<div id="divorced" class="half" style="display:none">
<input type="text" name="edate_<?php echo $spouse['individual_id']?>" id="edate_<?php echo $spouse['individual_id']?>" value="<?php echo $spouse['end_date']; ?>" placeholder="Rašykite skyrybų datą čia"/>
<?php echo $this->formError->error("edate_".$spouse['individual_id'].""); ?>
</div>
</div>
<?php
}
?>
<p class="submit">
<input class="first-btn" type="submit" id="edit-relationship" name="edit-relationship" value="Edit"/>
</p>
</form>
jQuery("#divorced_option").click(function() {
document.getElementById("divorced").style.display = "inline-block";
});
jQuery("#married_option").click(function() {
document.getElementById("divorced").style.display = "none";
});
我想知道的是,当您不知道其全名时,如何检查是否单击了一个单选按钮,只有一半。例如,#divorced_option
不是全名,它应该是这样的#divorced_option_161, #divorced_option_161...
因此,代码应检查是否单击了名称中包含divorced_option
的单选按钮。有没有办法实现这个目标?
EDIT
它适用于jQuery("[id^='married_option']").click(function(){})
。但是我忘了在原来的问题中提到离婚的元素也不是全名,它应该是divorced_161,divorced_162,这取决于点击的单选按钮的名称。如何隐藏与单选按钮匹配的元素?
答案 0 :(得分:4)
使用attribute starts with selector(例如[name^="value"]
):
jQuery("[id^='divorced_option']").click(function() {
document.getElementById("divorced").style.display = "inline-block";
});
jQuery("[id^='married_option']").click(function() {
document.getElementById("divorced").style.display = "none";
});
答案 1 :(得分:0)
“Attribute Contains”选择器在这里很有用,这意味着您的代码将是:
jQuery("[id*='divorced_option']").click(function() {
document.getElementById("divorced").style.display = "inline-block";
});
jQuery("[id*='married_option']").click(function() {
document.getElementById("divorced").style.display = "none";
});
答案 2 :(得分:0)
您可以选择基于#divorced_option_[number]
模式的元素,同时检查它们是否被选中:
$("input[id*=divorced_option]:checked");
如果您想检查divorced_option
子字符串是否在广播元素的name=""
属性中,请将id
从选择器更改为name
,如这样:
$("input[name*=divorced_option]:checked");
在此之后,只需检查是否jQuery返回了一个实际的元素。